Lazy filtering
We've seen multiple ways to build sequences so far—now let's introduce a real need for them. When you filter sequences, each value from the collection is passed through the predicate function and into the side-effect, one at a time.
Basic lazy filtering
To help illustrate the concept of lazy filtering, let's implement a basic predicate function with some extra logging:
const predicate = (item) => { console.log('filtering', item); return item % 2; };
This logging will make it easy for us to see when the actual filtering of a value happens relative to when the value is used in the side-effect. Let's try this on an indexed sequence:
const myList = List.of(1, 2, 3, 4, 5, 6); myList .toSeq() .filter(predicate) .forEach(item => console.log('myList', item)); // -> filtering 1 // -> myList 1 // -> filtering 2 // -> filtering 3 // -> myList 3 // -> filtering 4 // -> filtering 5 // -> myList 5 // -> filtering 6
If you pay close...