Functional programming tools.
- Copyright:
- Mat. 2018-present
- License:
- Apache-2.0
- Source:
Methods
(inner) Y(f) → {function}
Y-combinator (fixed point of f
).
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
f: recf => (...args) => { ... recf(...args) ... } |
Returns:
- Type:
-
function
(inner) app(f) → {function}
Basic function application.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
Funcion
|
Returns:
- Type:
-
function
x => f(x)
(inner) choose(key, choicesopt, defaultChoiceopt, argsopt) → {Out|DefaultOut|undefined}
Functional replacement of a switch
statement. When used properly (i.e.
choices
object is prepared once and stored in memory for later accesses)
then for large choice sets it should be noticeably faster than plain
switch
statement - it's semantics require sequential evaluation, so
it's time complexity, in general case, tends to be linear. Here, there is
no requirement of sequential evaluation, so average time complexity should
be no worse than logarithmic.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
|||
choices |
<optional> |
Plain JS object in form |
|
defaultChoice |
<optional> |
Simple JS function in form |
|
args |
<optional> |
If choice functions accepts arguments, then put an array of appropriate values here. |
Returns:
- Type:
-
Out
|DefaultOut
|undefined
Return value of choosen or default function.
(inner) compose(…fs) → {function}
Function composition - read as "compose backward" or "but first".
let:
f: X -> Y, g: Y -> Z
then:
g(f(x)) <=> (g . f) (x) <=> compose(g, f) (x)
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fs |
function
|
<repeatable> |
Returns:
- Type:
-
function
(inner) curry(f) → {CurryFun}
Return curried form of a given function f
.
If funcion f
has arity 3, and g = curry(f)
then
a following invocations have the same result:
g(a, b, c)
g(a, b) (c)
g(a) (b, c)
g(a) (b) (c)
Function f
's arity is obtained by checking it's .length
property, so if function f
is defined with a rest parameter
then this parameter is excluded. Also, only parameters before
the first one with a default value are included.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Returns:
- Type:
-
CurryFun
(inner) curryN(n, f) → {CurryFun}
Translate the evaluation of function f
taking n
arguments
into an evaluation of sequence of n
functions, where each
next function is a result of previous function evaluation.
f(a, b, c, d, e) <=> curryN(5, f) (a) (b) (c) (d) (e)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
n |
Number
|
|
f |
function
|
Returns:
- Type:
-
CurryFun
(inner) curryThunk(f) → {ThunkFun}
Translate the evaluation of function f
taking multiple arguments
into an evaluation of sequence of functions, each with a single argument.
Because curryThunk
doesn't assume anything on passed function
f
arity, final invocation has to be done with no arguments.
f(a, b, c, d) <=> curryThunk(f) (a) (b) (c) (d) ()
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Returns:
- Type:
-
ThunkFun
(inner) flow(…fs) → {function}
Function composition - read as "compose forward" or "and then".
let:
f: X -> Y, g: Y -> Z
then:
g(f(x)) <=> (g . f) (x) <=> flow(f, g) (x)
Inspired by https://github.com/tfausak/flow.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fs |
function
|
<repeatable> |
Returns:
- Type:
-
function
(inner) handleException(fn, handleropt) → {any}
Handle exceptions in expressions.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
function
|
||
handler |
function
|
<optional> |
Returns:
- Type:
-
any
(inner) identity(x) → {any}
Return value passed as a first argument.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
any
|
Returns:
- Type:
-
any
(inner) lazyish(x) → {function}
Put a given argument under function abstraction.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
any
|
Returns:
- Type:
-
function
(inner) local(()) → {*}
Local binding.
Inspired by https://github.com/kongware/scriptum
- Source:
Parameters:
Name | Type | Description |
---|---|---|
() |
function
|
=> T |
Returns:
- Type:
-
*
T
(inner) locker(nopt) → {function}
Create function that can "lock the thing".
During the first n
invocations returned function acts as identity.
During the n+1
invocation the argument thing
is memoized
and on all subsequent invocations passed arguments are ignored
and memoized thing
is returned.
let lock = func.locker()
lock("I like you!")
'I like you!'
lock("I hate you.")
'I like you!'
let lock2 = func.locker(2)
lock2("Repeat after me!")
'Repeat after me!'
lock2(42)
42
lock2("All right...")
42
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
n |
Number
|
<optional> |
1 |
Returns:
- Type:
-
function
(any) => any
(inner) partial(f) → {function}
Partial application.
Bind init
arguments to function f
and construct
a function of smaller arity which accept rest
of the arguments.
Example:
let f = (a, b) => a + b
f(3, 4) -> 7
let g = partial(f) (3) // note that `partial` is in *curried* form
g(4) -> 7
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Returns:
- Type:
-
function
(inner) pipe(…args) → {function}
Function composition - read as "compose forward" or "and then".
Version of flow
taking arguments (args
) first
and then functions (fs
).
let:
f: X -> Y, g: Y -> Z
then:
g(f(x)) <=> (g . f) (x) <=> pipe(x) (f, g)
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
args |
function
|
<repeatable> |
Returns:
- Type:
-
function
(inner) rearg(f) → {function}
Function arguments rearrangement.
Takes function f
and indices
and returns a new function,
which has it's arguments arranged according to indices
.
Returned function will expect the number of arguments to be
no less than the number of indices
. If not all of the required
arguments will be passed, a new function will be returned
expecting rest of the arguments.
In other words - function returned by rearg
is curried.
Example:
string.padLeft("Foo", 10, ".") -> ".......Foo"
let rePad = func.rearg(string.padLeft) (1, 2, 0) // *curried* form
rePad(10, ".", "Bar") -> ".......Bar"
console.log("a", "b", "c", "d", "e")
a b c d e
let revConsole = func.rearg(console.log) (4, 3, 2, 1, 0)
revConsole("a", "b", "c", "d", "e")
e d c b a
revConsole("f") ("g", "h") ("i") ("j")
j i h g f
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Returns:
- Type:
-
function