ConSequence.js
ConSequence.js - a library providing functional helpers for generators
examples
// Generator containing the sequence of natural numbers.{let i = 1;while truei++;}// Sequence of all Mersenne numbers.{return cons;}// Sequence of all Mersenne numbers that are prime.{{return cons;}return cons;}cons; // [3, 7, 31];
methods
fromArray(arr)
Utility function that creates a generator from a given array arr
.
let g = cons;console; // prints 1 2
toArray(it)
Utility function that consumes the whole sequence it
and returns an array
containing all of its values.
{1;2;3;}cons; // [1, 2, 3]
range(from, to)
Creates a generator containing an arithmetic progression starting with the value
from
up to including value to
.
cons; // [1, 2, 3]
map(it, fun)
Creates a generator containing the results of applying fun
to all values of
it
.
{return x * x;}cons; // [1, 4, 9]cons; // [1, 4, 9]
filter(it, p)
Creates a generator containing values of it
where the predicate p
holds.
{return x % 2;}cons; // [1, 3, 5]cons; // [1, 3]
reject(it, p)
Creates a generator containing values of it
where the predicate p
does
not hold.
{return x % 2;}cons; // [2, 4, 6]cons; // [2, 4]
compact(it)
Creates a generator containing all truthy values of it
.
cons; // [-1, 1]cons; // [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 cons;}; // 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.cons;cons;
min(it)
Consumes the whole sequence it
and returns the minimum value found or
+Infinity
for empty sequences.
cons; // 1cons; // 1
max(it)
Consumes the whole sequence it
and returns the maximum value found or
-Infinity
for empty sequences.
cons; // 6cons; // 7
uniq(it)
Returns a generator containing all distinct values from the given sequence it
.
{return x % 3;}cons; // [1, 2, 0]cons; // ['a', 'b', 'c']
every(it, fun)
Returns true
if the given predicate fun
holds for all values of the given
sequence it
. Returns true
as well for empty sequences.
{return x > 0;}cons; // truecons; // falsecons true);
some(it, fun)
Returns true
if the given predicate fun
holds for at least one value of the
given sequence it
. Returns false
for empty sequences.
{return x === 0;}cons; // falsecons; // truecons false);
size(it)
Consumes the whole sequence it
and returns the number of values found.
conssizecons; // 5conssize1 2 3; // 3
contains(it, val)
Returns true
if the given value val
is contained in the given sequence it
,
false
otherwise.
cons; // truecons; // false
find(it, fun)
Returns the first value of the given sequence it
for which the given predicate
fun
holds.
{return x > 3;}cons; // 4cons; // 4cons; // undefined
take(it, num)
Returns a generator containing the first num
values from the given sequence
it
.
cons; // [1, 2, 3]cons; // [1, 2]
drop(it, num)
Returns a generator containing all but the first num
values from the given
sequence it
.
cons; // [4, 5, 6]cons; // [2, 3]
flatten(it)
Flattens the given (nested) sequence it
.
{1;2;3 4;}cons; // [0, 1, 2, 3, 4]cons; // [1, 2, 3, 4]
flatMap(it, fun)
Creates a generator containing the flattened results of applying fun
to all
values of it
.
{return x x;}cons; // [1, 1, 2, 2, 3, 3]cons; // [1, 1, 2, 2, 3, 3]
install
For node.js, with npm do:
npm install consequence
todos
license
MIT