SeqJS
contains operations for working with values of type Seq
which is a container of a series of elements. Types of
a seq's elements are not fixed. Individual seq elements are computed only as required.
Seq
conforms iterable
and iterator
protocols of JavaScript.
A seq instance can be constructed as follows:
// An empty sequence
let seq = new Seq();
console.log([...seq]);
// => [];
// A sequence wrapped an array
let seq = new Seq([1, 2, 3]);
console.log([...seq]);
// => [1, 2, 3]
// A sequence wrapped a generator function
let seq = new Seq(function * () {
yield 1;
yield 2;
yield 3;
});
console.log([...seq]);
// => [1, 2, 3]
// A sequence wrapped an iterator
const iterator = {
i: 1,
[Symbol.iterator] () {
return this;
},
next () {
if (this.i <= 3) {
return { value: this.i++, done: false };
}
return { done: true };
},
};
let seq = new Seq(iterator);
console.log([...seq]);
// => [1, 2, 3]
Seq is multi-iterable.
let seq = new Seq([1, 2, 3]);
for (const item of seq) {}
for (const item of seq) {}
console.log([...seq]);
console.log([...seq]);
// => [1, 2, 3]
// => [1, 2, 3]
Seq is closable.
let seq = new Seq([1, 2, 3, 4, 5]);
for (const item of seq) {
if (item == 2) break;
}
console.log([...seq]);
// => [1, 2, 3, 4, 5]
Please read the API Docs for full details.
Nyi Nyi Than - @nyinyithann
- Exploring ES6 By Dr. Axel Rauschmayer
- Understanding ECMAScript 6 By Nicholas C. Zakas
- Collection Pipeline by Martin Fowler
- javascript.info
- Iteration protocols
- F# Seq Module
MIT