Module lua_fun
Functional programming library for lua lang.
Info:
- Copyright: IMAIA, 2019
- License: MIT
- Author: Italo Maia
Local Functions
totable (gen) | evaluates a one or two values generator into a table creates an array for a single value generator and a regular table otherwise |
generator (t) | creates a generator function for the items of `t` |
keys (t) | creates a generator function for the keys of `t` |
values (t) | creates a generator function for the values of `t` |
call (fn, ...) | calls a function with the provided argument also useful to force call a function reference |
compose (fn_a, fn_b) | creates a new function that calls `fn_a` with the output of `fn_b` |
flip (fn) | creates a new function that inverts the argument order very useful to work around api incompatibilities |
get (index, t) | gets the value of `t` in the `index` position |
factory (fn[, fnc=function[, init]]) | generates values while condition is met |
map (fn, t) | map function applies `fn` once to each value of `t`; return value is a generator. |
filter (fn, t) | fp filter function creates a new array where each value is the result of calling `fn` against a value of `t` if it is not falsy. |
lambda (desc) | shortcut for creating an inline function with implicit return
syntax: | |
reduce (fn, t[, init]) | fp reduce function reduces `t` to a single element by applying `fn` against each value of `t` in pairs. |
zip (...) | puts together each element of `t1` with a corresponding element of each table in `...` for each key of `t1`, puts the value of `t1[key]` and `tx[key]`, where tx is a table from `...` together in an array. |
partial (fn, ...) | creates a new function with new defaults |
pick (index, ...) | picks the nth item from a multi value input |
memoize (fn[, cache]) | creates a function that returns the same value given the same input cache doesn't work if return value is nil |
Local Functions
- totable (gen)
-
evaluates a one or two values generator into a table
creates an array for a single value generator and a regular table otherwise
Parameters:
- gen function
Returns:
-
table
Raises:
if `gen` is not a function - generator (t)
-
creates a generator function for the items of `t`
Parameters:
- t table
Returns:
-
function () callable that returns each value in `t` once
Raises:
if `t` is not tableUsage:
gen = generator({2, 3, 4}); assert(({gen()})[2] == 2); assert(({gen()})[2] == 3); assert(({gen()})[2] == 4);
- keys (t)
-
creates a generator function for the keys of `t`
Parameters:
- t table
Returns:
-
function () callable that returns each value in `t` once
Raises:
if `t` is not tableUsage:
gen = generator({2, 3, 4}); assert(gen() == 2); assert(gen() == 3); assert(gen() == 4);
- values (t)
-
creates a generator function for the values of `t`
Parameters:
- t table
Returns:
-
function () callable that returns each value in `t` once
Raises:
if `t` is not tableUsage:
gen = generator({2, 3, 4}); assert(gen() == 2); assert(gen() == 3); assert(gen() == 4);
- call (fn, ...)
-
calls a function with the provided argument
also useful to force call a function reference
Parameters:
- fn function arguments passed to `fn`
- ... arguments passed to `fn`
Returns:
-
any
- compose (fn_a, fn_b)
-
creates a new function that calls `fn_a` with the output of `fn_b`
Parameters:
- fn_a function
- fn_b function
Returns:
-
new composed function
- flip (fn)
-
creates a new function that inverts the argument order
very useful to work around api incompatibilities
Parameters:
- fn function
Returns:
-
new function with parameters order inverted
Usage:
pow2 = partial(flip(math.pow), 2)
- get (index, t)
-
gets the value of `t` in the `index` position
Parameters:
- index int
- t table
Returns:
-
any
Usage:
assert(get(2, {5, 6, 7}) == 6)
- factory (fn[, fnc=function[, init]])
-
generates values while condition is met
Parameters:
- fn function (i, v) generates value series; receives init or last call output as input
- fnc function (i, v) inclusive condition of continuity; if omitted, factory never stops (default function)
- init any (optional)
Usage:
factory(L'|i,v| v + 1', L'|i,v| v < 10')
- map (fn, t)
-
map function
applies `fn` once to each value of `t`; return value is a generator.
Parameters:
- fn function (v) called once for each value of `t`
- t array
Returns:
-
new array with the new values
Usage:
map({1, 2, 3}, tostring) -- {'1', '2', '3'}
- filter (fn, t)
-
fp filter function
creates a new array where each value is the result
of calling `fn` against a value of `t` if it is not falsy.
Parameters:
- fn function (v) called once for each value of `t`
- t array
Returns:
-
new array with values for cases where `fn(value)` was not falsy
Usage:
filter({1, 2, 3}, function (v) return v < 3 end) -- {1, 2}
- lambda (desc)
-
shortcut for creating an inline function with implicit return
syntax: |
| Parameters:
- desc string function description
Returns:
-
function new function
Usage:
lambda('|p1,p2| p1+p2') -- function(p1, p2) return p1 + p2 end
- reduce (fn, t[, init])
-
fp reduce function
reduces `t` to a single element by applying `fn` against
each value of `t` in pairs. The first pair is always
the value of `init` against the first item of `t`. Keep
in mind that if not provided `init` is nil.
Parameters:
- fn function (v1, v2)
- t array
- init number (optional)
Usage:
reduce({1, 2, 3}, function (a, b, 0) return a + b end) -- 6
- zip (...)
-
puts together each element of `t1` with a corresponding element of each table in `...`
for each key of `t1`, puts the value of `t1[key]` and `tx[key]`, where
tx is a table from `...` together in an array. If `t1` and `tx` are uneven
or keys in `t1` are not found in `tx`, the value of `t1` for such cases will be
an array with less than `#{...} + 1` elements.
Parameters:
- ... variable number of arrays
Returns:
-
table with values of each table grouped by index
Usage:
for x, y, z in zip({'a', 'b'}, {'x', 'y'}, {5, 6}) do print(x, y, z) -- a x 5 then b y 6 end
- partial (fn, ...)
-
creates a new function with new defaults
Parameters:
- fn function
- ...
Returns:
Usage:
local fn = partial(function (a, b) return a*b end, 2) assert(fn(2) == 4) assert(fn(3) == 6)
- pick (index, ...)
-
picks the nth item from a multi value input
Parameters:
- index int
- ...
Returns:
Usage:
assert(pick(2, 5, 6, 7) == 6)
- memoize (fn[, cache])
-
creates a function that returns the same value given the same input
cache doesn't work if return value is nil
Parameters:
- fn function
- cache table provide your own instance for custom cache behavior (optional)
Returns:
-
...