Implements common array methods in a lazy manner. Nothing more.
There's an asynchronous version of this library as well, called lazily-async. See https://github.com/isotropy/lazily-async
npm install lazily
import { Seq } from "lazily";
const seq = Seq.of([1, 2, 3])
for (const i of seq) {
console.log(i)
}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();Seq.of([1, 2, 3])
.toArray()
// [1, 2, 3]Seq.of([1, 2, 3])
.map(x => x * 2)
.toArray()
// [2, 4, 6]Seq.of([1, 2, 3])
.flatMap(x => [x*10, x*20])
.toArray()
// [11, 21, 12, 22, 13, 23]Seq.of([1, 2, 3, 4])
.filter(x => x > 2)
.toArray()
//[3, 4]Seq.of([1, 2, 3, 4, 5])
.exit(x => x > 3)
.toArray()
// [1, 2, 3]Seq.of([1, 2, 3, 4, 5])
.exitAfter(x => x > 3)
.toArray()
// [1, 2, 3, 4]Seq.of([1, 2, 3, 4, 5])
.find(x => x * 10 === 30)
// 3Seq.of([1, 2, 3, 4, 5])
.reduce((acc, x) => acc + x, 0)
// 15Seq.of([1, 2, 3, 4, 5])
.reduce((acc, x) => acc + x, 0, acc => acc > 6)
// 10Seq.of([1, 2, 3, 4, 5])
.first();
// 1Seq.of([1, 2, 3, 4, 5])
.first(x => x > 3);
// 4Seq.of([1, 2, 3, 4, 5])
.last();
// 5Seq.of([1, 2, 3, 4, 5])
.last(x => x < 3);
// 2Seq.of([1, 2, 3, 4, 5])
.every(x => x <= 5);
// trueSeq.of([1, 2, 3, 4, 5])
.some(x => x === 3);
// trueSeq.of([1, 2, 3, 4, 5])
.includes(3);
// trueSeq.of([1, 2, 3, 4, 5])
.concat(Seq.of([6, 7, 8)]))
.toArray();
// [1, 2, 3, 4, 5, 6, 7, 8]Seq.of([1, 2, 3, 4, 5])
.reverse()
.toArray();
// [5, 4, 3, 2, 1]Seq.of([1, 2, 3, 4, 5])
.slice(2, 4)
.toArray();
// [3, 4, 5]