Chimes
Array.prototype
iteration methods for any iterator.
The new iterable data structures in ES6 are hard to justify using as they're severely lacking in expressive power when compared to Arrays. This library's goal is to add all the expressive power of Arrays to anything iterable, including generators.
Current implementation is porcelain for future sugar.
Installation
npm install chimes
Requires an ES6 transpiler such as 6to5, traceur or a polyfill like core-js.
Usage
Most methods work just as they do for Array.prototype
, though they
take the iterable as the first argument and always return an iterator.
Note that even every
, some
& reduce
return iterators – the last value in the iterator is the value you'd expect from the corresponding Array.prototype
method.
An iterable is any object that implements Symbol.iterator
or a generator.
const map = let set = setsetset let doubleSet = console // falseconsole // trueconsole // trueconsole // trueconsole// [2,4,6]
API
Methods Implemented
- map
- filter
- every
- some
- reduce
- concat
Helpers
- first
- last
map(iterable, fn, context)
Creates a new Iterator with the results of lazily calling fn
on every
element in the supplied iterable
.
Array.prototype.map for Iterables.
const map = let set = setsetset let doubleSet = console // falseconsole // trueconsole // trueconsole // trueconsole// [2,4,6]
Parameters
iterable: Iterable
, Creates a new Iterator with the results of lazily calling fn
on every
element in the supplied iterable
.
fn: function
, Will be invoked with: value, index & iteration object
context: Mixed
, Optional. Value to use as this when executing fn
.
Returns: Iterator
filter(iterable, fn, context)
Creates a new Iterator with all elements in the supplied iterable
that
pass the test implemented by fn
. fn
is applied lazily.
Array.prototype.filter for iterables.
const filter = let value % 2 === 0let size = 5let evenNumbers = Array // [ 4, 2, 0 ]
Parameters
iterable: Iterable
, Creates a new Iterator with all elements in the supplied iterable
that
pass the test implemented by fn
. fn
is applied lazily.
fn: function
, Will be invoked with: value, index & iteration object
context: Mixed
, Optional. Value to use as this when executing fn
.
Returns: Iterator
every(iterable, fn, context)
Creates a new Iterator which tests whether all elements that have passed
through the iterator thus far pass the test implemented by fn
.
fn
is applied lazily.
Array.prototype.every for iterables.
const every = let value % 2 === 0let size = 5let allEven = let notAllEven = Array // trueArray // false
Parameters
iterable: Iterable
, Creates a new Iterator which tests whether all elements that have passed
through the iterator thus far pass the test implemented by fn
.
fn
is applied lazily.
fn: function
, Will be invoked with: value, index & iteration object
context: Mixed
, Optional. Value to use as this when executing fn
.
Returns: Iterator
some(iterable, fn, context)
Creates a new Iterator which tests whether any elements that have passed
through the iterator thus far pass the test implemented by fn
.
fn
is applied lazily.
Array.prototype.some for iterables.
const some = let value % 2 === 0let allEven = let someEven = let noneEven = Array // trueArray // trueArray // false
Parameters
iterable: Iterable
, Creates a new Iterator which tests whether any elements that have passed
through the iterator thus far pass the test implemented by fn
.
fn
is applied lazily.
fn: function
, Will be invoked with: value, index & iteration object
context: Mixed
, Optional. Value to use as this when executing fn
.
Returns: Iterator
reduce(iterable, fn, initialValue)
Creates a new Iterator that lazily applies fn
against an accumulator.
Each step of the iteration reduces it to a single value.
Array.prototype.reduce for iterables.
const reduce = let init + valuelet total = )let otherTotal = ) Array // 6Array // 7
Parameters
iterable: Iterable
, Creates a new Iterator that lazily applies fn
against an accumulator.
Each step of the iteration reduces it to a single value.
fn: function
, Will be invoked with: previousValue, currentValue, index & iteration object.
initialValue: Mixed
, Optional. Object to use as the first argument to the first call of fn
.
Returns: Iterator
concat(iterable, )
Creates a new Iterator comprising of the first iterable joined with the second iterable.
Array.prototype.concat for iterables.
const concat = let set = 123let array = 456let joined = for let item in joined console// 1// 2// 3// 4// 5// 6
Parameters
iterable: Iterable
, Creates a new Iterator comprising of the first iterable joined with the second iterable.
concat: Iterable
, Creates a new Iterator comprising of the first iterable joined with the second iterable.
Array.prototype.concat for iterables.
Returns: Iterator
last(iterable)
Creates a new Iterator that simply contains the last element of iterable
.
const last = for let item in console // 6
Parameters
iterable: Iterable
, Creates a new Iterator that simply contains the last element of iterable
.
Returns: Iterator
first(iterable)
Creates a new Iterator that simply contains the first element of iterable
.
const first = for let item in console // 2
Parameters
iterable: Iterable
, Creates a new Iterator that simply contains the first element of iterable
.
Returns: Iterator
License
MIT