4 ms·
Any pratical application for yield that you couldn't do just as simply with a for loop? I'm not criticizing, I'm just curious.
by aston 19y ago
Any pratical application for yield that you couldn't do just as simply with a for loop? I'm not criticizing, I'm just curious.
- geebee 19y agoWell, I can't guarantee that this is was a good use of block and yield, or even if it was advisable, but here's what I did one time... I needed to construct a bunch of polynomials from a set of 20 odd coordinates (x,y) coordinates. I decided to use langrange interpolating polynomials, and I wanted the polynomial to match three of the twenty points (otherwise they bounce all over the place). The idea was then to select the polynomial that minimized the least squares difference with the other points that weren't used to create the curve(I'm not sure if this counts as practical, since there are other approaches to polynomial curve-fitting that would have been more appropriate). Anyway, I had a method that evaluated the least squares, and then I constructed the polynomial as a block of code and yielded to it in the iteration that evaulated all the point for the least-squares fit... There are other ways to do it (always are) - I could have broken the polynomials into terms and reconstructed them in the method, for instance - but block and yield were very convenient in this case.