Categories: .Net, P#
Posted by
mheydt on
3/26/2006 4:41 PM |
Comments (0)
The Iterate Pattern Revisited
Consider the previous pattern for iteration that I put forth:
int[] ia = new int[] { 5, 4, 3, 2, 1};
iterate ia |i| { Console.WriteLine(i); };
I was thinking the other night that it would be nice to provide a criteria for selection of elements for processing as it might be very likely that you may not want to apply code to every element. My thoughts extend this to the following code snippet:
int[] ia = new int[] { 5, 4, 3, 2, 1};
iterate ia |i| do { Console.WriteLine(i); } where { i < 3; };
The do keyword precedes a code block (anonymous method / delegate) that is applied to all elements that match the criteria specified by the code block specified after the where keyword.
Functionally, I would think the runtime would iterate across each element, applying the where code block to each, and in the case of a true condition, then apply the do code to that element. Additionally, perhaps it might be better to process the where code on all element first build a set of items to process, and then iterate across all of the result set with the do code block.
f87ca430-6f0b-4d77-8d91-0a23373bc29f|0|.0