-
Notifications
You must be signed in to change notification settings - Fork 0
Iterum
Xavier Garcia Buils edited this page Mar 5, 2017
·
2 revisions
iterum
library aims to provide a lazy iterable class Iterum
that has a set of inmutable methods inspired in Array javascript methods and underscore/lodash functions.
Iterable interface introduced by ES2015 version allows an easy way to implement Array-like lazy methods by the aid of generators. However an object that implements Iterable interface is just able to use for..of statement and spread operator that are eager operations. Iterum class
builds iterables that has these Array-like methods and, then, you can work transparently with lazy iterables like arrays.
- Node.js >=6
- ES2015 transpilers
$ npm install iterum --save
const Iterum = require('iterum')
const array = [6, 2]
const set = new Set()
.add(5)
.add(4)
const lazyIterable = Iterum.range(1, 7, 2) // potentially [1, 3, 5, 7]
.concat(array, set) // potentially [1, 3, 5, 7, 6, 2, 5, 4]
.map(value => 2 * value) // potentially [2, 6, 10, 14, 12, 4, 10, 8]
.filter(value => value < 10) // potentially [2, 6, 4, 8]
// Then,
// obtaining the array of values:
[...lazyIterable] // returns [2, 6, 4, 8]
// traversing values:
for (let val of lazyIterable) {
// ...
}
// creating an iterator that traverses the values
let iterator = lazyIterable[Symbol.iterator]()
iterator.next() // {value: 2, done: false}
iterator.next() // {value: 6, done: false}
iterator.next() // {value: 4, done: false}
iterator.next() // {value: 8, done: false}
iterator.next() // {value: undefined, done: true}
Iterum
allows to build just what you need. Read customized build section for more information
- xgbuils (Author)
MIT