lazily
Implements common array methods in a lazy manner. Nothing more.
There's an async version of this library as well, called lazily-async.
See https://github.com/isotropy/lazily-async
Installation
npm install lazily
Create a sequence
import { Seq } from "lazily";
const seq = Seq.of([1, 2, 3])
for (const i of seq) {
console.log(i)
}
It's lazy
Sequences are lazy. For example, in the following example only one map() action is performed irrespective of the length of the sequence.
const seq = Seq.of([1, 2, 3])
.map(x => x * 2)
.first();
toArray()
Seq.of([1, 2, 3]).toArray()
map(fn)
Seq.of([1, 2, 3])
.map(x => x * 2)
.toArray()
filter(predicate)
Seq.of([1, 2, 3, 4])
.filter(x => x > 2)
.toArray()
exit(predicate)
Seq.of([1, 2, 3, 4, 5])
.exit(x => x > 3)
.toArray()
exitAfter(predicate)
Similar to exit(), but includes the last item as well.
Seq.of([1, 2, 3, 4, 5])
.exitAfter(x => x > 3)
.toArray()
find(predicate)
Seq.of([1, 2, 3, 4, 5]).find(x => x * 10 === 30)
reduce(fn)
Seq.of([1, 2, 3, 4, 5])
.reduce((acc, x) => acc + x, 0)
short-circuited reduce(fn, initialValue, stopPredicate)
Seq.of([1, 2, 3, 4, 5])
.reduce((acc, x) => acc + x, 0, acc => acc > 6)
first()
Seq.of([1, 2, 3, 4, 5]).first();
first(predicate)
Seq.of([1, 2, 3, 4, 5]).first(x => x > 3);
last()
Seq.of([1, 2, 3, 4, 5]).last();
last(predicate)
Seq.of([1, 2, 3, 4, 5]).last(x => x < 3);
every(predicate)
Seq.of([1, 2, 3, 4, 5]).every(x => x <= 5);
some(predicate)
Seq.of([1, 2, 3, 4, 5]).some(x => x === 3);
includes(item)
Seq.of([1, 2, 3, 4, 5]).includes(3);
concat(seq)
Seq.of([1, 2, 3, 4, 5]).concat(Seq.of([6, 7, 8])).toArray();
reverse()
Seq.of([1, 2, 3]).reverse().toArray();
slice(begin, end)
Seq.of([1, 2, 3, 4, 5, 6]).slice(2, 4).toArray();