yield.js
library providing functional helpers for generators
Please note that this module currently requires node.js v0.11.2 (or higher)
started with --use-strict
and --harmony
as it makes heavy use of generators,
sets and block-scoped variables.
example
// Generator containing the sequence of natural numbers.{let i = 1;while truei++;}// Sequence of all Mersenne numbers.{return y;}// Sequence of all Mersenne numbers that are prime.{{return y;}return y;}y; // [3, 7, 31];
methods
fromArray(arr)
Utility function that creates a generator from a given array arr
.
let g = y;yole; // prints 1 2
toArray(it)
Utility function that yumes the whole sequence it
and returns an array
containing all of its values.
{1;2;3;}y; // [1, 2, 3]
range(from, to)
Creates a generator containing an arithmetic progression starting with the value
from
up to including value to
.
y; // [1, 2, 3]
map(it, fun)
Creates a generator containing the results of applying fun
to all values of
it
.
{return x * x;}y; // [1, 4, 9]y; // [1, 4, 9]
filter(it, p)
Creates a generator containing values of it
where the predicate p
holds.
{return x % 2;}y; // [1, 3, 5]y; // [1, 3]
reject(it, p)
Creates a generator containing values of it
where the predicate p
does
not hold.
{return x % 2;}y; // [2, 4, 6]y; // [2, 4]
compact(it)
Creates a generator containing all truthy values of it
.
y; // [-1, 1]y; // [1, 2, true]
reduce(it, fun)
Consumes the whole sequence it
and reduces all of its values down to a single
value. fun
is called on each step with the current reduction state and value
as arguments.
{return y;}; // 9; // 6; // 0
each(it, fun)
Consumes the whole sequence it
calling the given function fun
for every
value, passing the value as a single argument.
// Show alert boxes for numbers 1 to 3.y;y;
min(it)
Consumes the whole sequence it
and returns the minimum value found or
+Infinity
for empty sequences.
y; // 1y; // 1
max(it)
Consumes the whole sequence it
and returns the maximum value found or
-Infinity
for empty sequences.
y; // 6y; // 7
uniq(it)
Returns a generator containing all distinct values from the given sequence it
.
{return x % 3;}y; // [1, 2, 0]y; // ['a', 'b', 'c']
every(it, p)
Returns true
if the given predicate p
holds for all values of the given
sequence it
. Returns true
as well for empty sequences.
{return x > 0;}y; // truey; // falsey; // true
some(it, p)
Returns true
if the given predicate p
holds for at least one value of the
given sequence it
. Returns false
for empty sequences.
{return x === 0;}y; // falsey; // truey; // false
size(it)
Consumes the whole sequence it
and returns the number of values found.
ysizey; // 5ysize1 2 3; // 3
contains(it, val)
Returns true
if the given value val
is contained in the given sequence it
,
false
otherwise.
y; // truey; // false
find(it, p)
Returns the first value of the given sequence it
for which the given predicate
p
holds.
{return x > 3;}y; // 4y; // 4y; // undefined
take(it, num)
Returns a generator containing the first num
values from the given sequence
it
.
y; // [1, 2, 3]y; // [1, 2]
drop(it, num)
Returns a generator containing all but the first num
values from the given
sequence it
.
y; // [4, 5, 6]y; // [2, 3]
flatten(it)
Flattens the given (nested) sequence it
.
{1;2;3 4;}y; // [0, 1, 2, 3, 4]y; // [1, 2, 3, 4]
flattenOnce(it)
Flattens the given (nested) sequence it
by a single level.
y; // [1, 2, 3, [[4]]]
flatMap(it, fun)
Creates a generator by applying fun
to all values of it
and using the
elements of the resulting sequences.
{return x x;}y; // [1, 1, 2, 2, 3, 3]y; // [1, 1, 2, 2, 3, 3]y; // [1, 1, [2], [2]]
union(it [, it2, ...])
Creates a generator containing the union of all passed sequences.
y; // [1, 2, 3, 4, 5, 6]
difference(it, oth [, oth2, ...])
Creates a generator containing all values of it
that are not contained in
oth
and all further given sequences.
y; // [5, 6]
without(it, val [, val2, ...])
Creates a generator containing all values of it
that do not match val
or
any other of the given values.
y; // [3, 4, 5, 6]y; // [3, 4]
partition(it, p)
Partitions the given sequence it
according to the given predicate p
. It
returns an array with the first entry containing a generator with all values of
it
for which the predicate p
holds. The second entry will contain a
generator with all values for which the predicate does not hold.
{return x > 3;}y; // [[4, 5, 6], [1, 2, 3]]y; // [[4, 5], [2, 3]]
install
For node.js, with npm do:
npm install yield
todos
- Use yield* when implemented.
- Use for...of when implemented.
- Use rest parameters when implemented.
license
MIT