Sub topic 7
Skipping Values with drop
This chapter assumes you know the iteration protocol, which says an iterator returns { value, done } from next(). It also assumes you know that helpers are lazy and that take limits output. drop(limit) is the opposite of take. It returns a new iterator that skips the first limit values of its source and then passes every later value through unchanged.
Basic usage
function* naturals() {
let n = 0;
while (true) yield n++;
}
console.log(naturals().drop(3).take(4).toArray()); // [3, 4, 5, 6]
console.log([1, 2].values().drop(5).toArray()); // []When limit is larger than the number of available values, drop produces an empty result without an error. You also do not need a finite source. In the first example, drop skips 0, 1, and 2 from an infinite generator, and take stops the sequence after four values.
When the skipping happens
Creating drop(3) does not touch the source. On the first call to next(), the drop helper calls the source's next() limit times, discards those results, and then pulls one more value to return. Later calls go straight through to the source.
Skipped values are still computed
Dropped values are not avoided. They are produced by the source and then thrown away. Any helper placed before drop in the chain still runs its callback for them:
const it = [10, 20, 30, 40]
.values()
.map((x) => {
console.log('map', x);
return x;
})
.drop(2);
console.log('created');
console.log(it.next().value);
// created
// map 10
// map 20
// map 30
// 30If the callback is expensive and you don't need its results for the skipped items, put drop before map. Then the callback runs only on the values you keep. The output stays the same, but the work done is different.
Pagination with drop and take
Using drop and then take gives you a window into a sequence. This is useful for reading one page of results without building an array of all the earlier items:
function* naturals() {
let n = 0;
while (true) yield n++;
}
function page(iter, pageIndex, pageSize) {
return iter.drop(pageIndex * pageSize).take(pageSize).toArray();
}
console.log(page(naturals(), 2, 5)); // [10, 11, 12, 13, 14]About the limit
drop checks limit when you call it, not when you first pull a value. A negative number or NaN throws a RangeError. Infinity is allowed and skips everything. The Argument Validation chapter covers the exact rules.
Exercise
Given const it = ['a', 'b', 'c', 'd'].values().drop(1).filter((s) => s !== 'c');, what does it.toArray() return, and how many times is the filter callback called?
Show answer
It returns ['b', 'd']. The filter callback runs three times, for 'b', 'c', and 'd'. It never sees 'a', because drop discards 'a' before filter runs.
Exercise
In source.map(expensive).drop(1000).take(10), how many times does expensive run, and how could you reduce that?
Show answer
It runs 1010 times: 1000 times for dropped values and 10 times for kept values. Rewriting the pipeline as source.drop(1000).map(expensive).take(10) gives the same output with only 10 calls.