I recently built a linq alternative in typescript for my clientside javascript. I know there are many open source items available but I wanted to build it myself for the challenge. I started to look into lazy evaluation (iterators) my methods. I'm starting to struggle with the benefits.
If I have something like this.
var myArray = [1,2,3,4,5];
current eager evaluation
var myResult = myArray.Where(function(x){ x >= 4 });
//my result will be 4,5 - then I can run a Last
var myLastItem = myResult.Last();
my last item = 5.
If I combine them, I could do the following. The last method would only loop through 4,5 (filtered from the where) So it wouldn't need to loop through the entire record set (myArray)
var myLastItem = myArray.Where(function(x){ x >= 4 }).Last();
if we try to lazy evaluate this with iterators how would this help at all?
The only benefit is if we had an if statement where we might need the result - or may not. This way the linq statements are never ran.