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
function* nat() {
let i = 1;
while (true) {
yield i++;
}
}
function mersenneNumbers() {
return y.map(nat(), function (x) { return Math.pow(2, x) - 1 });
}
function mersennePrimes() {
function isPrime(n) {
return y.every(y.range(2, n - 1), function (x) { return n % x });
}
return y.filter(y.drop(mersenneNumbers(), 1), isPrime);
}
y.toArray(y.take(mersennePrimes(), 3));
methods
fromArray(arr)
Utility function that creates a generator from a given array arr
.
let g = y.fromArray([1, 2]);
yole.log(g.next().value, g.next().value);
toArray(it)
Utility function that yumes the whole sequence it
and returns an array
containing all of its values.
function* g() {
yield 1;
yield 2;
yield 3;
}
y.toArray(g());
range(from, to)
Creates a generator containing an arithmetic progression starting with the value
from
up to including value to
.
y.toArray(y.range(1, 3));
map(it, fun)
Creates a generator containing the results of applying fun
to all values of
it
.
function square(x) {
return x * x;
}
y.toArray(y.map(y.range(1, 3), square));
y.toArray(y.map([-1, -2, -3], square));
filter(it, p)
Creates a generator containing values of it
where the predicate p
holds.
function odd(x) {
return x % 2;
}
y.toArray(y.filter(y.range(1, 6), odd));
y.toArray(y.filter([1, 2, 3, 4], odd));
reject(it, p)
Creates a generator containing values of it
where the predicate p
does
not hold.
function odd(x) {
return x % 2;
}
y.toArray(y.reject(y.range(1, 6), odd));
y.toArray(y.reject([1, 2, 3, 4], odd));
compact(it)
Creates a generator containing all truthy values of it
.
y.toArray(y.compact(y.range(-1, 1)));
y.toArray(y.compact([1, "", 2, 0, true, false]));
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.
function sum(it) {
return y.reduce(it, function (acc, x) { return acc + x }, 0);
}
sum([2, 3, 4]);
sum(y.range(1, 3));
sum(y.range(-1, 1));
each(it, fun)
Consumes the whole sequence it
calling the given function fun
for every
value, passing the value as a single argument.
y.each(y.range(1, 3), alert);
y.each([1, 2, 3], alert);
min(it)
Consumes the whole sequence it
and returns the minimum value found or
+Infinity
for empty sequences.
y.min(y.range(1, 6));
y.min([5, 1, 7]);
max(it)
Consumes the whole sequence it
and returns the maximum value found or
-Infinity
for empty sequences.
y.max(y.range(1, 6));
y.max([5, 1, 7]);
uniq(it)
Returns a generator containing all distinct values from the given sequence it
.
function mod3(x) {
return x % 3;
}
y.uniq(y.map(y.range(1, 9), mod3));
y.uniq(['a', 'b', 'a', '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.
function greaterThanZero(x) {
return x > 0;
}
y.every(y.range(1, 5), greaterThanZero);
y.every([0, 1, 2, 3, 4], greaterThanZero);
y.every([], greaterThanZero);
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.
function equalToZero(x) {
return x === 0;
}
y.some(y.range(1, 5), equalToZero);
y.some([0, 1, 2, 3, 4], equalToZero);
y.some([], equalToZero);
size(it)
Consumes the whole sequence it
and returns the number of values found.
y.size(y.range(1, 5));
y.size([1, 2, 3]);
contains(it, val)
Returns true
if the given value val
is contained in the given sequence it
,
false
otherwise.
y.contains(y.range(0, 5), 0);
y.contains([1, 2, 3, 4, 5], 0);
find(it, p)
Returns the first value of the given sequence it
for which the given predicate
p
holds.
function greaterThanThree(x) {
return x > 3;
}
y.find(y.range(1, 6), greaterThanThree);
y.find([3, 4, 5], greaterThanThree);
y.find([], greaterThanThree);
take(it, num)
Returns a generator containing the first num
values from the given sequence
it
.
y.take(y.range(1, 6), 3);
y.take([1, 2, 3], 2);
drop(it, num)
Returns a generator containing all but the first num
values from the given
sequence it
.
y.drop(y.range(1, 6), 3);
y.drop([1, 2, 3], 1);
flatten(it)
Flattens the given (nested) sequence it
.
function* g() {
yield 1;
yield 2;
yield [3, 4];
}
y.flatten([0, g()]);
y.flatten([1, [2], [3, [[4]]]]);
flattenOnce(it)
Flattens the given (nested) sequence it
by a single level.
y.flattenOnce([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.
function map(x) {
return [x, x];
}
y.flatMap(y.range(1, 3), map);
y.flatMap([1, 2, 3], map);
y.flatMap([1, [2]], map);
union(it [, it2, ...])
Creates a generator containing the union of all passed sequences.
y.union(y.range(1, 3), [2, 3, 4], y.range(1, 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.difference(y.range(1, 6), [3, 4], y.range(1, 2));
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.without(y.range(1, 6), 1, 2);
y.without([1, 2, 3, 4], 1, 2);
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.
function greaterThanThree(x) {
return x > 3;
}
y.partition(y.range(1, 6), greaterThanThree);
y.partition([2, 3, 4, 5], greaterThanThree);
install
For node.js, with npm do:
npm install yield
todos
license
MIT