| Author: | Roman Neuhauser |
|---|---|
| Contact: | neuhauser@sigpipe.cz |
| Copyright: | This document is in the public domain. |
Contents
Array callback is a function which takes up to three arguments:
function (val, ind, arr)
These functions are used by many array algorithms.
Comparison function is a binary function which returns an integer greater than, equal to, or less than 0, according as lhs is greater than, equal to, or less than rhs.
Shallow copy c of an array a is an array such that c[N] == a[Nth set element]. That is, c is always consecutive, even if a contains "holes". c.length == a.length is true only if a is consecutive, and c == a is never true.
Predicate is a function which returns values convertible to bool.
The Equivalency sections, where present, follow a certain set of rules, most of which should be obvious. These things are worth stressing:
The Possible Implementation sections, where included, display a possible (usually actual) implementation(s) of the function using other Zeta functions.
fun is a function. binders is an array of functions. bind returns a variadic function such that calling it with arguments a1, a2, ..., aN (args for brevity) is the same as calling fun(binders[0](args), binders[1](args) ... binders[binders.length - 1](args)).
In other words, bind returns a function which wraps fun, bind's first argument. When called, the wrapper returned from bind doesn't just pass its arguments to fun, it uses binders to generate fun's arguments: binders[0] supplies the first argument, binders[1] second one, and so on. Each binder is called with the arguments the wrapper is being called with. fun is then called with as many positional arguments as there are binders.
Equivalency:
bind(fun, [f, g, h])(x, y, z) == fun(f(x, y, z), g(x, y, z), h(x, y, z))
See Also: $1, $2, $3, argv, bind1st, bind2nd, binder, value.
fun is a binary function. lhs is any value. bind1st returns a unary function which calls fun with lhs as the first argument. Returns fun's return value.
Equivalency:
bind1st(fun, lhs)(rhs) == fun(lhs, rhs)
See Also: bind, bind2nd, binder.
Example:
Print powers of 2:
for_(
map(bind1st(pow, 2), range(1, 16))
, use1st(print)
);
fun is a binary function. rhs is any value. bind2nd returns a unary function which calls fun with rhs as the second argument. Returns fun's return value.
Equivalency:
bind2nd(fun, rhs)(lhs) == fun(lhs, rhs)
See Also: bind, bind1st, binder.
Example:
Print numbers raised to the second power:
for_(
map(bind2nd(pow, 2), range(1, 16))
, use1st(print)
);
fun is a function. binder returns a variadic function which returns fun with all arguments bound from its argument list. Returns fun's return value.
Equivalency:
binder(fun)(x, y, z)(a, b, c) == bind(fun, map(value, [z, y, x]))(a, b, c)
fun is a unary function. collect returns a variadic function such that calling it with arguments a1, a2, ..., aN is the same as calling fun([a1, a2, ..., aN]). Returns fun's return value.
Equivalency:
collect(fun)(x, y, z) == fun([x, y, z])
Possible implementation:
var collect = bind2nd(compose, argv);
f and g are functions. compose returns a variadic function such that calling it with arguments a1, a2, ..., aN is the same as calling f(g(a1, a2, ..., aN)).
Equivalency:
compose(f, g)(x, y, z) == f(g(x, y, z))
See Also: $1, $2, $3, argv, bind, bind1st, bind2nd, binder, composex.
Possible implementation:
var compose = function(f, g)
{
return bind(f, [g]);
}
Example:
Find the index of the first element with false truth value:
var arr = [2, 'f', 0, 1, -3]; assertEquals(2, find_if(not, arr));
fs is an array of functions. composex returns a variadic function such that calling it with arguments a1, a2, ..., aN is the same as calling $N(fs[0](fs[1](...fs[fs.length - 1](a1, a2, ... aN)))). When fs is empty, composex degenerates into $N.
composex is a generalization of the compose function adapter.
Equivalency:
composex([f, g, h])(x, y, z) == $N(f(g(h(x, y, z)))) composex([])(x, y, z) == $N(x, y, z) == z
See Also: $1, $2, $3, $N, bind, bind1st, bind2nd, binder, compose.
Possible implementation:
var composex = bind(reduce, [value(compose), $1, value(itself)]);
f is a function, v is a value. curry returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling f(v, a1, a2, ... aN).
arr is an array. sep is any value, defaults to a comma. join returns a string of all arr elements with sep between every two elements. join relies on arr.join, including its behavior when sep is omitted.
Equivalency:
join(arr, sep) == arr.join(sep) join(arr) == arr.join()
Example:
assertEquals('a-b-c', join(['a', 'b', 'c'], '-'));
that is an object. fun is a function. method returns a variadic function such that calling it with arguments a1, a2, ..., aN is the same as calling fun(a1, a2, ..., aN) with this set to that. Returns fun's return value.
Equivalency:
method(that, fun)(x, y, z) == fun.call(that, x, y, z)
fun is a function. negate returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling !fun(a1, a2, ... aN).
Equivalency:
negate(f)(x, y, z) == !f(x, y, z)
See Also: not.
Possible implementation:
var negate = bind1st(compose, not);
str is an array. sep is any value. split returns an array of all maximal nonoverlapping str substrings separated with sep. split relies on str.split, including its behavior when sep is omitted.
Equivalency:
split(str, sep) == str.split(sep) split(str) == str.split()
See Also: join, joiner, splitter.
Example:
var rv = split('a-b-c', '-');
assertEquals(3, rv.length);
assertEquals('a', rv[0]);
assertEquals('b', rv[1]);
assertEquals('c', rv[2]);
fun is a function. spread returns a unary function which takes an array, and calls fun with positional arguments supplied from that array. Returns fun's return value.
Equivalency:
spread(fun)([x, y, z]) == fun(x, y, z)
Possible implementation:
var spread = bind1st(bind1st, apply);
Example:
var emps = [
[1, "John"],
[2, "Mary"],
[3, "Eric"],
];
var display = function (id, name)
{
print("Employee no. " + id + " is " + name);
}
for_(emps, spread(display));
fun is a function. to_bool returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling Boolean(fun(a1, a2, ... aN)).
Equivalency:
to_bool(f)(x, y, z) == Boolean(f(x, y, z))
See Also: compose.
Possible implementation:
var to_bool = bind1st(compose, Boolean);
fun is a function. to_num returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling Number(fun(a1, a2, ... aN)).
Equivalency:
to_num(f)(x, y, z) == Number(f(x, y, z))
See Also: compose.
Possible implementation:
var to_num = bind1st(compose, Number);
fun is a unary function. use1st returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling fun(a1).
Equivalency:
use1st(f)(x, y, z) == compose(f, $1)(x, y, z) == f(x)
Possible implementation:
var use1st = bind2nd(compose, $1);
fun is a unary function. use1st returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling fun(a2).
Equivalency:
use2nd(f)(x, y, z) == compose(f, $2)(x, y, z) == f(y)
See Also: $1, $2, $3, argv, compose, use1st.
Possible implementation:
var use2nd = bind2nd(compose, $2);
fs is an array of functions. conjoin returns a variadic predicate which calls each function in fs in turn relaying arguments until one returns false or until fs is exhausted. Returns false if one of the functions returned false, otherwise returns the return value of the last fs element. Returns true if fs is empty.
conjoin is a functional generalization of the && operator.
Equivalency:
conjoin([f, g, h])(x, y, z) == true && f(x, y, z) && g(x, y, z) && h(x, y, z)
See Also: disjoin.
fs is an array of functions. disjoin returns a variadic predicate which calls each function in fs in turn relaying arguments until one returns true or until fs is exhausted. Returns true if one of the functions returned true, otherwise returns the return value of the successful fs element. Returns false if fs is empty.
disjoin is a functional generalization of the || operator.
Equivalency:
disjoin([f, g, h])(x, y, z) == false || f(x, y, z) || g(x, y, z) || h(x, y, z)
See Also: conjoin.
p is a predicate. t is a function. f is a function. ifte returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling t(a1, a2, ... aN) or f(a1, a2, ... aN), depending on the value of p(a1, a2, ... aN).
Equivalency:
ifte(p, t, f) == function ()
{
if (apply(p, arguments)) {
return apply(t, arguments);
} else {
return apply(f, arguments);
}
}
Possible implementation:
var ifte = function (p, t, f)
{
return function ()
{
var c = bind2nd(apply, arguments);
return c(c(p) ? t : f)
}
}
arr is an array. insert returns a function (val, ind) which assigns val to arr[ind]. Returns val.
Equivalency:
insert(arr)(v, i) == (arr[i] = v)
See Also: push.
sep is any value. joiner returns a unary function such that calling it with argument arr is the same as calling join(arr, sep).
Equivalency:
joiner(sep)(arr) == bind2nd(join, sep)(arr) == join(arr, sep)
See Also: join, split, splitter.
Possible implementation:
var joiner = bind1st(bind2nd, join);
arr is an array or object. nth returns a function (ind) which returns arr[ind].
Equivalency:
nth(arr)(i) == arr[i]
See Also: member, project, select.
Possible implementation:
var nth = bind1st(bind1st, member);
arr is an array. push returns a variadic function which pushes its arguments into arr. Returns the new length of arr.
Equivalency:
push(arr)(x, y, z) == arr.push(x, y, z)
Possible implementation:
var push = bind(method, [$1, select('push')]);
sep is any value. splitter returns a unary function such that calling it with argument arr is the same as calling split(arr, sep).
See Also: join, joiner, split.
Equivalency:
splitter(sep)(arr) == bind2nd(split, sep)(arr) == split(arr, sep)
Possible implementation:
var splitter = bind1st(bind2nd, split);
val is any value. value returns a function which returns val. Reference types are not copied.
Equivalency:
value(v)(x, y, z) == v
fun is a function. args is an array. fun is called with positional arguments supplied from args. apply returns fun's return value.
Equivalency:
apply(fun, [x, y, z]) == fun(x, y, z)
See Also: spread.
Example:
var args = [
[0, 5],
[3, 3, 2],
[3, 3, -3],
[-3, 4]
];
var ranges = map(spread(range), args);
argv is a variadic function which returns a shallow copy of its arguments object as an array.
Equivalency:
argv(x, y, z) == [x, y, z]
See Also: $1, $2, $3, use1st, use2nd.
Possible implementation:
function argv()
{
return map(itself, arguments);
}
arr is an array. op is an array callback. op is called once for each element in arr.
The range of elements processed is set before the first invocation of callback. Elements which are appended to the array after the call to for_ begins will not be visited by fun. If existing elements of arr are changed or deleted, their value as passed to fun will be the value at the time for_ visits them; elements that are deleted are not visited.
Example:
Print all elements of an array:
for_(
['foo', 'bar', 'baz']
, use1st(print)
);
cond is a nullary predicate. fun is a nullary function. while_ trivially wraps a while statement, that is, it calls fun until cond returns false.
... represents variable number of arrays. chain returns a concatenation of all arrays in ....
Equivalency:
chain([a, b, c], [d, e, f]) == [a, b, c, d, e, f]
arr is an array. sz is an integer. chunk returns arr split into chunks of sz elements each. The last chunk may contain fewer elements.
See Also: group_by.
a is an array. v is a value. cons returns a shallow copy of a with v prepended.
arr is an array. copy returns a shallow copy of arr.
Equivalency:
copy(arr) == arr.concat()
Possible implementation:
var copy = bind1st(map, itself);
var copy = bind2nd(method, select('concat'));
lhs is an array of numbers. rhs is an array of numbers. inner_product returns the sum of multiplications of respective elements of lhs and rhs.
More precisely, inner_product first initializes result with 0, and then, for each pair lhs[i], rhs[i], rv is updated with rv + lhs[i] * rhs[i].
Example:
assertEquals(35, inner_product([1, 3], [5, 10]));
pred is an array callback predicate. arr is an array. pred is called once with each element in arr. filter returns an array of those elements of arr for which pred returned a value convertible to true.
fun is an array callback, expected to return values convertible to integers. arr is an array. group_by calls fun once for each element in arr. group_by returns a (possibly sparse) array such that values returned by fun are used for keys, and values are arrays of arr elements which generated that particular key.
obj is an object. items returns an array of obj's [value, key] pairs.
Equivalency:
items({a: 10, b: 20, c: 30}) == [[10, 'a'], [20, 'b'], [30, 'c']]
See Also: properties.
arr is an array. keys returns an array of indexes in arr which have values assigned to them.
Equivalency:
keys(arr) == map($2, arr)
Possible implementation:
var keys = bind1st(map, $2);
fun is an array callback. arr is an array. fun is called once for each element in arr. map returns an array of results.
See Also: copy, filter, reduce.
Example:
Compute Fibonnaci numbers:
var data = [0, 2, 4, 6, 8];
var exp = [1, 2, 5, 13, 34];
for_(
map(fib, data)
, function (v, i)
{
assertEquals(exp[i], v);
}
);
arr is an array. product returns the result of multiplication of all arr elements.
Equivalency:
product([x, y, z]) == reduce(mul, [x, y, z], 1) == 1 * x * y * z product([]) == reduce(mul, [], 1) == 1
obj is an object. properties returns an array of obj's property names.
See Also: items.
fun is a binary function, arr is an array. init is any value. reduce is a generalization of summation: it computes the sum (or some other binary operation) of init and all elements of arr.
fun is not required to be either commutative or associative: the order of all of reduce's operations is specified. The result is first initialized to init. Then, for each element in arr, in order from beginning to end, it is updated by result = fun(result, elem).
arr is an array. reverse returns a shallow copy of arr with elements in reverse order.
arr is an array, start is an integer, zero-based index into arr, end is an integer, zero-based index into arr. slice returns a shallow copy of the range denoted by start, end.
slice relies on arr.slice, including its behavior when end is not provided.
Equivalency:
slice(arr, start, end) == arr.slice(start, end)
arr is an array, cmp is a comparison function. sorted returns a shallow copy of arr sorted according to cmp.
sorted relies on Array.prototype.sort, including its behavior when cmp is not provided.
arr is an array. sum returns the result of addition of all arr elements.
Equivalency:
sum([x, y, z]) == reduce(plus, [x, y, z], 0) == 0 + x + y + z sum([]) == reduce(plus, [], 0) == 0
pred is an unary predicate. arr is an array. take_while copies elements of arr until pred(elem) returns false.
eqfun is a binary predicate. arr is an array. unique returns a shallow copy of arr such that every time a consecutive group of identical elements appears in arr, unique copies only the first element.
arrs is an array of arrays. zip returns an array of arrays such that the nth element contains the nth element from each input array. The returned array has as many elements as the shortest input array.
Example:
var a1 = [100, 101, 102, 103, 200];
var a2 = ['a', 'b', 'c', 'd'];
var rv = zip([a1, a2]);
assertEquals(min(a1.length, a2.length), rv.length);
assertEquals(100, rv[0][0]);
assertEquals('a', rv[0][1]);
cnt is a natural number. val is any value. fill returns an array of cnt elements with val for values. Reference types are not copied.
Possible implementation:
var fill = bind(map, [use2nd(value), iota]);
cnt is an integer. iota returns an array of cnt integers such that rv[i] == i.
iota(5) == [0, 1, 2, 3, 4]
start is an integer. cnt is an integer. step is an integer, defaults to 1. range returns an array of cnt integers such that rv[i] == start + (step * i).
range(0, 5, 2) == [0, 2, 4, 6, 8] range(0, 5, 1) == [0, 1, 2, 3, 4] range(0, 5) == [0, 1, 2, 3, 4]
arr is an array, pred is an array callback. pred is called with each element in arr as long as it returns true, until arr is exhausted or pred returns false. every returns true if pred returned true for all elements, false otherwise. Returns true if arr is empty.
Example:
Test whether all numbers in an array are positive:
var is_pos = bind1st(lt, 0); assertEquals(true, every(is_pos, [1, 2, 3])); assertEquals(false, every(is_pos, [1, -2, 3]));
arr is an array, pred is an array callback. pred is called with each element in arr as long as it returns false until arr is exhausted or pred returns true. some returns false if pred returned false for all elements, true otherwise. Returns false if arr is empty.
Example:
Test whether any element in an array is 0:
var is_0 = bind1st(eq, 0); assertEquals(true, some(is_0, [1, 2, 0, 3])); assertEquals(false, some(is_0, [1, 2, 3]));
val is a value. arr is an array. contains returns true iff val == arr[i] for any i in arr.
Equivalency:
contains(v, a) == some(bind1st(eq, v), a)
Possible implementation:
var contains = bind(lt, [value(-1), find]); var contains = compose(bind1st(lt, -1), find); var contains = bind(some, [bind1st(bind1st, eq), $2]); var contains = to_bool(compose(bind1st(plus, 1), find)); var contains = composex([Boolean, bind1st(plus, 1), find]);
val is any value, arr is an array. find returns the first index of val in arr, -1 if not found.
pred is an array callback, arr is an array. pred is called once with each element in arr until it returns true. find_if returns the index of the element in arr for which pred returned true or -1 if there was no such element.
values is an array. dflt is an optional default return value. coalesce returns first non-null element of values, or, if no such element is found, dflt when given, and null otherwise.
init is any value. previous returns a unary function which returns init on the first call, and its argument from the previous call on all subsequent ones.
init is a number. dec returns a function such that calling it is the same as --init.
lhs is any value. rhs is any value. div is a trivial wrapper around the intrinsic / operator.
See Also: intdiv.
Equivalency:
div(x, y) == (x / y)
init is a number. inc returns a function such that calling it is the same as ++init.
lhs is a number. rhs is a number. intdiv returns the floored quotient of lhs and rhs.
See Also: div.
Equivalency:
intdiv(lhs, rhs) == Math.floor(lhs / rhs)
Possible implementation:
var intdiv = compose(Math.floor, div);
lhs is any value. rhs is any value. minus is a trivial wrapper around the intrinsic - operator.
Equivalency:
minus(x, y) == (x - y)
lhs is any value. rhs is any value. mod is a trivial wrapper around the intrinsic % operator.
Equivalency:
mod(x, y) == (x % y)
lhs is any value. rhs is any value. mul is a trivial wrapper around the intrinsic * operator.
Equivalency:
mul(x, y) == (x * y)
lhs is any value. rhs is any value. plus is a trivial wrapper around the intrinsic + operator.
Equivalency:
plus(x, y) == (x + y)
lhs is any value. rhs is any value. compare returns an integer greater than, equal to, or less than 0, according as lhs is greater than, equal to, or less than rhs.
Equivalency:
(compare(lhs, rhs) > 0) == (lhs > rhs) (compare(lhs, rhs) < 0) == (lhs < rhs) (compare(lhs, rhs) == 0) == (lhs == rhs)
lhs is any value, rhs is any value. eq returns true if lhs == rhs and false otherwise.
Equivalency:
eq(lhs, rhs) == (lhs == rhs)
Example:
Find the index of the first zero element in an array:
var arr = range(16, 9, -4); assertEquals(4, find_if(bind2nd(eq, 0), arr));
int is an integer. even returns true if int is even, false otherwise.
Equivalency:
even(int) == (0 == (int % 2))
See Also: odd.
Possible implementation:
var even = negate(odd);
Example:
var numbers = [ ... ]; var evens = filter(even, numbers);
Equivalency:
ge(lhs, rhs) == (lhs >= rhs)
Example:
Find the index of the first nonnegative element in an array:
var arr = range(-3, 7); assertEquals(3, find_if(bind2nd(ge, 0), arr));
Equivalency:
gt(lhs, rhs) == (lhs > rhs)
Example:
Find the index of the first positive element in an array:
var arr = range(-3, 7); assertEquals(4, find_if(bind2nd(gt, 0), arr));
val is any value. cls is a class. is_a returns true iff val instanceof cls.
Equivalency:
is_a(v, c) == v instanceof c
See Also: type_of.
val is any value. is_null returns true iff null == val.
Equivalency:
is_null(v) == (null == v)
Possible implementation:
var is_null = bind1st(eq, null);
Equivalency:
le(lhs, rhs) == (lhs <= rhs)
Example:
Find the index of the first nonpositive element in an array:
var arr = range(7, 7, -2); assertEquals(4, find_if(bind2nd(le, 0), arr));
Equivalency:
lt(lhs, rhs) == (lhs < rhs)
Example:
Find the index of the first negative element in an array:
var arr = range(3, 7, -1); assertEquals(4, find_if(bind2nd(lt, 0), arr));
lhs is any value. rhs is any value. max returns rhs iff rhs is greater than lhs, otherwise lhs. Reference types are not copied.
Equivalency:
max(lhs, rhs) == (lhs < rhs) ? rhs : lhs
Example:
Find the greatest value in an array:
assertEquals(7, reduce(max, [0, 7, -3, 5], Number.NEGATIVE_INFINITY));
lhs is any value. rhs is any value. min returns rhs iff rhs is less than lhs, otherwise lhs. Reference types are not copied.
Equivalency:
min(lhs, rhs) == (lhs > rhs) ? rhs : lhs
Example:
Find the smallest value in an array:
assertEquals(-3, reduce(min, [0, 7, -3, 5], Number.POSITIVE_INFINITY));
int is an integer. odd returns true if int is odd, false otherwise.
Equivalency:
odd(int) == (0 != (int % 2))
See Also: even.
Possible implementation:
var odd = to_bool(bind2nd(mod, 2));
Example:
var numbers = [ ... ]; var odds = filter(odd, numbers);
Returns false.
Equivalency:
false_() == false
Possible implementation:
var false_ = negate(true_);
val is any value. not returns val's logical complement.
Equivalency:
not(v) == !v
See Also: negate.
Returns true.
Equivalency:
true_() == true
Possible implementation:
var true_ = value(true);
$1 is a variadic function which returns its first argument.
Equivalency:
$1(x, y, z) == x
Possible implementation:
var $1 == project(0);
$2 is a variadic function which returns its second argument.
Equivalency:
$2(x, y, z) == y
Possible implementation:
var $2 == project(1);
$3 is a variadic function which returns its third argument.
Equivalency:
$3(x, y, z) == z
Possible implementation:
var $3 == project(2);
$N is a variadic function which returns its last argument.
Equivalency:
$N(x, y, z) == z
val is any value. itself returns val. Reference types are not copied.
Equivalency:
itself(v) == v
i is zero-based argument position. project returns a variadic function which returns its i'th argument.
Equivalency:
project(i)(x, y, z) == [x, y, z][i]
Possible implementation:
var project = bind(compose, [select, value(argv)]);
i is an array index. select returns a function which takes an array and returns its i'th member.
Equivalency:
select(i)(arr) == arr[i]
Possible implementation:
var select = bind1st(bind2nd, member);
arr is an array. empty returns true iff 0 == size(arr).
Equivalency:
empty(arr) == (0 == size(arr))
See Also: size.
Possible implementation:
var empty = bind(eq, [value(0), size]);
prop is an object property name or array index. obj is an object or array. in_ returns true iff prop in obj is true.
Equivalency:
in_(foo, bar) == (foo in bar)
See Also: member.
arr is an array. size returns arr.length.
Equivalency:
size(arr) == arr.length
See Also: empty.
Possible implementation:
var size = bind2nd(member, 'length');
obj is an array or an object. prop is an array index or property name. member returns obj[prop].
Equivalency:
member(o, i) == o[i]
See Also: nth.
Example:
var arr = range(0, 5); assertEquals(5, member(arr, 'length')); assertEquals(3, member(arr, 3));
cls is a class. new_ returns a variadic function such that calling it with arguments a1, a2, ... aN is the same as calling new cls(a1, a2, ... aN). The highest possible N is 10. Calling the function returned from new_ with more than 10 arguments will cause an exception.
Equivalency:
new_(cls)(x, y, z) == (new cls(x, y, z))
Example:
Create Date objects:
var dates = map(
new_(Date)
, range(1199142000000, 7, 86400000)
);
val is any value. type_of is a trivial wrapper around the intrinsic typeof operator.
See Also: is_a.