Comment by zelphirkalt
2 days ago
While you are right about being able to combine higher-order functions in the way you describe, I might not find your examples compelling, because they require 2 passes over the data.
On the other hand one could argue, that one pass of those is checking some condition (index below some number, or element followed by element that satisfies a predicate, or similar things), and the other pass is then blindly applying some other function, without having to check the condition again. Maybe that is in the end equal in performance.
> I might not find your examples compelling, because they require 2 passes over the data.
Are these the examples?
>> For example, if you want to fold over a partial data structure, you can use `filter` to select only the elements you want, and then fold over that subset. Or, if you want to exit early from a map, you can use `takeWhile` and only map over what's left.
These are done in a single pass. And if they weren't, I'd stop using them.
Only one pass over the data is made when using lazy evaluation, such as the Seq module in F#. For folds, you can also put the filter directly inside the accumulator function to avoid a second pass.