Loading topic
map, filter, take, and drop are methods on iterators, not on arrays. Before using them, you need a clear picture of what an iterator is, how it differs from an iterable, and which language features consume them. This chapter rebuilds that picture from the protocol up4 min read[Symbol.iterator]() method, and an iterator has a next() method that returns { value, done }. You should also be comfortable with prototypes and instanceof, and with generator functions.5 min readmap and filter methods, for...of loops, and generator functions (function*). It also builds on the iteration protocol from earlier chapters: an iterator is an object with a next() method that returns { value, done }.5 min readfor...of, spread syntax, and Array.prototype.map. It also builds on the previous chapter's point that helpers do nothing until a consumer pulls a value.5 min readnext() returning { value, done }), and how Array.prototype.filter behaves. It also builds on the idea from earlier chapters that iterator helpers are lazy: they do no work until something pulls a value.4 min readnext() returning { value, done }), generator functions, and the idea from earlier chapters that iterator helpers are lazy. You will learn what take does, how many values it pulls from its source, when it stops the source, and which 4 min read.values() or a generator function, and that you have seen map, filter, and take in earlier chapters. You should also know that toArray() collects whatever a pipeline produces into an array.4 min readmap, filter, take, and drop return new iterators. They describe work but do not do it. To get an actual result you need a consuming method. A consuming method pulls values from the iterator until it has an answer, then hands back a plain value instead of another iterator.4 min readmap, filter, take, and drop behave on their own, and that you can get results out with toArray. It also assumes you understand that iterator helpers are lazy. Here we look at what happens when you link them together: how values move through a chain4 min readfunction*, and that you have already seen map, filter, take, drop, and the consuming methods such as toArray and find. Here those pieces come together on sequences that never end. You will see which pipelines finish an4 min readmap, filter, take, drop), the consuming methods such as toArray, and how lazy pull-based evaluation works. It also assumes you are comfortable with the familiar Array.prototype methods. The goal is to choose between the two families ba4 min readmap and filter work on iterators and how to chain them into a pipeline. It also assumes you have used the index argument of Array.prototype.map. Iterator helpers have a similar argument, but it is called a counter, and it behaves differently in a few4 min readIterator.prototype, and how map, filter, take, and drop behave. Earlier chapters showed that the helpers are available only on objects whose prototype chain includes `Iterator.prototyp4 min readmap, filter, take, and drop work, and that you understand lazy evaluation: helper callbacks run only when a value is pulled with next(). You should also be comfortable with generator functions and try...catch.4 min readtake limit is reached, a for...of loop hits break, find gets a match, or a callback throws. In each of these cases the source may still hold resources such as file handles, timers, locks, or a generator with a pending finally block. This4 min readmap, filter, take, and drop, and that you understand how a pull-based pipeline runs one value at a time. It also assumes you know Array.prototype.flatMap, because the iterator version looks almost the same but behaves differently in some import4 min read