4 ms·
I haven't dealt with LINQ lately, but IIRC there was no way to do early termination on a sequence. // Not ideal list.Select(x => ExpensiveFunc(x)).Take(5);
by jeswin 7y ago
I haven't dealt with LINQ lately, but IIRC there was no way to do early termination on a sequence.
// Not ideal
list.Select(x => ExpensiveFunc(x)).Take(5);
Transducers can be terminated early - https://dev.to/greencoder/build-your-own-transducer-and-impress-your-cat---part-4-2bp8 https://dev.to/greencoder/build-your-own-transducer-and-impr...
- kevingadd 7y agoIEnumerable implicitly offers early termination. LINQ expressions are always implicit by default, so .Select(...).Take(n) will stop after taking N items. It's just like a foreach loop (same interface/API). For the same reason, if you 'foreach' over the result of a linq query multiple times, it will evaluate the whole sequence again, which often isn't what you want - so people tend to call .ToArray() or .ToList() at the end to fully evaluate it once.
- jeswin 7y ago> IEnumerable implicitly offers early termination. LINQ expressions are always implicit by default, so .Select(...).Take(n) will stop after taking N items. My bad, indeed it does! (Embarassing, my job used to be working on LINQ-based ORM libs. Though more than a decade back.)
- fulafel 7y agoClojure's normal transducerless lazy sequences are fine for early-terminating sequences.. mostly. They're fine as long as you are ok with realizing up to 31 extra items. http://clojure-doc.org/articles/language/laziness.html#lazy-sequences-chunking http://clojure-doc.org/articles/language/laziness.html#lazy-...