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