The question is pretty simple: is there any way to call Array methods like filter
, find
, map
, etc. not only on arrays, but on any iterable?
filter, find, map etc make sense not only on an array, but generally on a sequence. And iterable is a sequence that can be oterated over, so it makes sense to filter a sequence, find (the first element in a sequence), map elements of a sequence... whatever the sequence is.
Imagine such case: an infinite generator (such as fibonacci sequence, generator returns one item at a time). I want to find the first element that satisfies a given condition. Using spread like this:
[...fib()].find(conditionFunction)
will first make fib sequence to be dumped, which results in crashing the browser because of memory consumption (infinite sequence). What I could do is to manually call for loop and use conditionFunction inside.
Is there any way to call filter, find, map, etc lazily on (non-array) iterables?