Module moses
Utility-belt library for functional programming in Lua.
Source on Github
Info:
- License: MIT
- Copyright: 2012-2014
- Author: Roland Yonaba
- Release: 1.4.0
Table functions
each (t, f[, ...]) | Iterates on each key-value pairs in a table. |
eachi (t, f[, ...]) | Iterates on each integer key-value pairs in a table. |
at (t, ...) | Returns an array of values at specific indexes and keys. |
count (t[, value]) | Counts occurrences of a given value in a table. |
countf (t, f[, ...]) | Counts occurrences validating a predicate. |
cycle (t, n) | Iterates through a table and loops n times. |
map (t, f[, ...]) | Maps function f(key, value) on all key-value pairs. |
reduce (t, f[, state]) | Reduces a table, left-to-right. |
reduceRight (t, f[, state]) | Reduces a table, right-to-left. |
mapReduce (t, f[, state]) | Reduces a table while saving intermediate states. |
mapReduceRight (t, f[, state]) | Reduces a table while saving intermediate states. |
include (t, value) | Search for a value in a table. |
detect (t, value) | Search for a value in a table. |
contains (t, value) | Checks if a value is present in a table. |
findWhere (t, props) | Returns the first value having specified keys props . |
select (t, f[, ...]) | Selects and extracts values passing an iterator test. |
reject (t, f[, ...]) | Clones a table while dropping values passing an iterator test. |
all (t, f[, ...]) | Checks if all values in a table are passing an iterator test. |
invoke (t, method[, ...]) | Invokes a method on each value in a table. |
pluck (t, a) | Extracts property-values from a table of values. |
max (t[, transform[, ...]]) | Returns the max value in a collection. |
min (t[, transform[, ...]]) | Returns the min value in a collection. |
shuffle (t[, seed]) | Returns a shuffled copy of a given collection. |
same (a, b) | Checks if two tables are the same. |
sort (t[, comp]) | Sorts a table, in-place. |
groupBy (t, iter[, ...]) | Splits a table into subsets. |
countBy (t, iter[, ...]) | Groups values in a collection and counts them. |
size ([...]) | Counts the number of values in a collection. |
containsKeys (t, other) | Checks if all the keys of other table exists in table t . |
sameKeys (tA, tB) | Checks if both given tables have the same keys. |
Array functions
toArray ([...]) | Converts a vararg list to an array-list. |
find (array, value[, from]) | Looks for the first occurrence of a given value in an array. |
reverse (array) | Reverses values in a given array. |
selectWhile (array, f[, ...]) | Collects values from a given array. |
dropWhile (array, f[, ...]) | Collects values from a given array. |
sortedIndex (array, the[, comp[, sort]]) | Returns the index at which a value should be inserted. |
indexOf (array, the) | Returns the index of a given value in an array. |
lastIndexOf (array, the) | Returns the index of the last occurrence of a given value. |
addTop (array, ...) | Adds all passed-in values at the top of an array. |
push (array, ...) | Pushes all passed-in values at the end of an array. |
pop (array[, n]) | Removes and returns the values at the top of a given array. |
unshift (array[, n]) | Removes and returns the values at the end of a given array. |
pull (array, ...) | Removes all provided values in a given array. |
removeRange (array[, start[, finish]]) | Trims all values indexed within the range [start, finish] . |
chunk (array, f[, ...]) | Chunks together consecutive values. |
slice (array[, start[, finish]]) | Slices values indexed within [start, finish] range. |
first (array[, n]) | Returns the first N values in an array. |
initial (array[, n]) | Returns all values in an array excluding the last N values. |
last (array[, n]) | Returns the last N values in an array. |
rest (array[, index]) | Trims all values before index. |
compact (array) | Trims all falsy (false and nil) values. |
flatten (array[, shallow]) | Flattens a nested array. |
difference (array, another) | Returns values from an array not present in all passed-in args. |
union (...) | Returns the duplicate-free union of all passed in arrays. |
intersection (array, ...) | Returns the intersection of all passed-in arrays. |
symmetricDifference (array, array2) | Performs a symmetric difference. |
unique (array) | Produces a duplicate-free version of a given array. |
isunique (array) | Checks if a given array contains distinct values. |
zip (...) | Merges values of each of the passed-in arrays in subsets. |
append (array, other) | Clones array and appends other values. |
interleave (...) | Interleaves arrays. |
interpose (value, array) | Interposes value in-between consecutive pair of values in array . |
range ([from[, to[, step]]]) | Produce a flexible list of numbers. |
rep (value, n) | Creates an array list of n values, repeated. |
partition. (array[, n]) | Iterator returning partitions of an array. |
permutation (array) | Iterator returning the permutations of an array. |
invert (array) | Swaps keys with values. |
concat (array[, sep[, i[, j]]]) | Concatenates values in a given array. |
Utility functions
identity (value) | Returns the passed-in value. |
once (f) | Returns a version of f that runs only once. |
memoize (f[, hash]) | Memoizes a given function by caching the computed result. |
after (f, count) | Returns a version of f that runs on the count-th call. |
compose (...) | Composes functions. |
pipe (value, ...) | Pipes a value through a series of functions. |
complement (f) | Returns the logical complement of a given function. |
juxtapose (value, ...) | Calls a sequence of passed-in functions with the same argument. |
wrap (f, wrapper) | Wraps f inside of the wrapper function. |
times (n, iter, ...) | Runs iter function n times. |
bind (f, v) | Binds v to be the first argument to function f . |
bindn (f, ...) | Binds ... to be the N-first arguments to function f . |
uniqueId ([template[, ...]]) | Generates a unique ID for the current session. |
Object functions
keys (obj) | Returns the keys of the object properties. |
values (obj) | Returns the values of the object properties. |
toBoolean (value) | Converts any given value to a boolean |
extend (destObj, ...) | Extends an object properties. |
functions ([obj]) | Returns a sorted list of all methods names found in an object. |
clone (obj[, shallow]) | Clones a given object properties. |
tap (obj, f[, ...]) | Invokes interceptor with the object, and then returns object. |
has (obj, key) | Checks if a given object implements a property. |
pick (obj, ...) | Return a filtered copy of the object. |
omit (obj, ...) | Return a filtered copy of the object. |
template (obj[, template]) | Fills nil properties in an object with the given template object. |
isEqual (objA, objB[, useMt]) | Performs a deep comparison test between two objects. |
result (obj, method[, ...]) | Invokes an object method. |
isTable (t) | Checks if the given arg is a table. |
isCallable (obj) | Checks if the given argument is an callable. |
isArray (obj) | Checks if the given argument is an array. |
isIterable (obj) | Checks if the given object is iterable with pairs (or ipairs ). |
isEmpty ([obj]) | Checks if the given is empty. |
isString (obj) | Checks if the given argument is a string. |
isFunction (obj) | Checks if the given argument is a function. |
isNil (obj) | Checks if the given argument is nil. |
isNumber (obj) | Checks if the given argument is a number. |
isNaN (obj) | Checks if the given argument is NaN (see Not-A-Number). |
isFinite (obj) | Checks if the given argument is a finite number. |
isBoolean (obj) | Checks if the given argument is a boolean. |
isInteger (obj) | Checks if the given argument is an integer. |
chain (value) | Returns a wrapped object. |
obj:value () | Extracts the value of a wrapped object. |
import ([context[, noConflict]]) | Imports all library functions into a context. |
Table functions
- each (t, f[, ...])
-
Iterates on each key-value pairs in a table. Calls function
f(key, value)
at each step of iteration.
Aliased asforEach
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
See also:
- eachi (t, f[, ...])
-
Iterates on each integer key-value pairs in a table. Calls function
f(key, value)
only on values at integer key in a given collection. The table can be a sparse array, or map-like. Iteration will start from the lowest integer key found to the highest one.
Aliased asforEachi
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
See also:
- at (t, ...)
-
Returns an array of values at specific indexes and keys.
Parameters:
- t table a table
- ... vararg A variable number of indexes or keys to extract values
Returns:
-
table
an array-list of values from the passed-in table
- count (t[, value])
-
Counts occurrences of a given value in a table. Uses isEqual to compare values.
Parameters:
- t table a table
- value value a value to be searched in the table. If not given, the size of the table will be returned
Returns:
-
number
the count of occurrences of
value
See also:
- countf (t, f[, ...])
-
Counts occurrences validating a predicate. Same as count, but uses an iterator.
Returns the count for values passing the test
f(key, value, ...)
Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
number
the count of values validating the predicate
See also:
- cycle (t, n)
-
Iterates through a table and loops
n
times. The full iteration loop will be repeatedn
times (or forever, ifn
is omitted). In casen
is lower or equal to 0, it returns an empty function.
Aliased asloop
.Parameters:
- t table a table
- n number the number of loops
Returns:
-
function
an iterator function yielding key-value pairs from the passed-in table.
- map (t, f[, ...])
-
Maps function
f(key, value)
on all key-value pairs. Collects and returns the results as a table.
Aliased ascollect
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
a table of results
- reduce (t, f[, state])
-
Reduces a table, left-to-right. Folds the table from the first element to the last element
to into a single value, with respect to a given iterator and an initial state.
The given function takes a state and a value and returns a new state.
Aliased asinject
,foldl
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(state, value)
- state state an initial state of reduction. Defaults to the first value in the table.
Returns:
-
state
state the final state of reduction
See also:
- reduceRight (t, f[, state])
-
Reduces a table, right-to-left. Folds the table from the last element to the first element
to single value, with respect to a given iterator and an initial state.
The given function takes a state and a value, and returns a new state.
Aliased asinjectr
,foldr
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(state,value)
- state state an initial state of reduction. Defaults to the last value in the table.
Returns:
-
state
state the final state of reduction
See also:
- mapReduce (t, f[, state])
-
Reduces a table while saving intermediate states. Folds the table left-to-right
to a single value, with respect to a given iterator and an initial state. The given function
takes a state and a value, and returns a new state. It returns an array of intermediate states.
Aliased asmapr
Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(state, value)
- state state an initial state of reduction. Defaults to the first value in the table.
Returns:
-
table
an array of states
See also:
- mapReduceRight (t, f[, state])
-
Reduces a table while saving intermediate states. Folds the table right-to-left
to a single value, with respect to a given iterator and an initial state. The given function
takes a state and a value, and returns a new state. It returns an array of intermediate states.
Aliased asmaprr
Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(state,value)
- state state an initial state of reduction. Defaults to the last value in the table.
Returns:
-
table
an array of states
See also:
- include (t, value)
-
Search for a value in a table. It does not search in nested tables.
Aliased asany
,some
Parameters:
- t table a table
- value value or function a value to search for
Returns:
-
boolean
a boolean :
true
when found,false
otherwiseSee also:
- detect (t, value)
-
Search for a value in a table. Returns the key of the value if found.
It does not search in nested tables.
Parameters:
- t table a table
- value value a value to search for
Returns:
-
key
the value key or nil
See also:
- contains (t, value)
-
Checks if a value is present in a table.
Parameters:
- t table a table
- value value a value to search for
Returns:
-
boolean
true if present, otherwise false
See also:
- findWhere (t, props)
-
Returns the first value having specified keys
props
.Parameters:
Returns:
-
value
a value from the passed-in table
- select (t, f[, ...])
-
Selects and extracts values passing an iterator test.
Aliased asfilter
.Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
the selected values
See also:
- reject (t, f[, ...])
-
Clones a table while dropping values passing an iterator test.
Aliased asdiscard
Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
the remaining values
See also:
- all (t, f[, ...])
-
Checks if all values in a table are passing an iterator test.
Aliased asevery
Parameters:
- t table a table
- f
function
an iterator function, prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
boolean
true
if all values passes the predicate,false
otherwise - invoke (t, method[, ...])
-
Invokes a method on each value in a table.
Parameters:
- t table a table
- method
function
a function, prototyped as
f(value, ...)
- ...
vararg
Optional extra-args to be passed to function
method
Returns:
-
result
the result(s) of method call
f(value, ...)
See also:
- pluck (t, a)
-
Extracts property-values from a table of values.
Parameters:
Returns:
-
table
an array of values for the specified property
- max (t[, transform[, ...]])
-
Returns the max value in a collection. If an transformation function is passed, it will
be used to extract the value by which all objects will be sorted.
Parameters:
- t table a table
- transform
function
an transformation function, prototyped as
transform(value,...)
, defaults to identity - ...
vararg
Optional extra-args to be passed to function
transform
Returns:
-
value
the maximum value found
See also:
- min (t[, transform[, ...]])
-
Returns the min value in a collection. If an transformation function is passed, it will
be used to extract the value by which all objects will be sorted.
Parameters:
- t table a table
- transform
function
an transformation function, prototyped as
transform(value,...)
, defaults to identity - ...
vararg
Optional extra-args to be passed to function
transform
Returns:
-
value
the minimum value found
See also:
- shuffle (t[, seed])
-
Returns a shuffled copy of a given collection. If a seed is provided, it will
be used to init the random number generator (via math.randomseed ).
Parameters:
- t table a table
- seed number a seed
Returns:
-
table
a shuffled copy of the given table
- same (a, b)
-
Checks if two tables are the same. It compares if both tables features the same values,
but not necessarily at the same keys.
Parameters:
Returns:
-
boolean
true
orfalse
- sort (t[, comp])
-
Sorts a table, in-place. If a comparison function is given, it will be used to sort values.
Parameters:
- t table a table
- comp
function
a comparison function prototyped as
comp(a,b)
, defaults to < operator.
Returns:
-
table
the given table, sorted.
- groupBy (t, iter[, ...])
-
Splits a table into subsets. Each subset feature values from the original table grouped
by the result of passing it through an iterator.
Parameters:
- t table a table
- iter
function
an iterator function, prototyped as
iter(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
iter
Returns:
-
table
a new table with values grouped by subsets
- countBy (t, iter[, ...])
-
Groups values in a collection and counts them.
Parameters:
- t table a table
- iter
function
an iterator function, prototyped as
iter(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
iter
Returns:
-
table
a new table with subsets names paired with their count
- size ([...])
-
Counts the number of values in a collection. If being passed more than one args
it will return the count of all passed-in args.
Parameters:
- ... vararg Optional variable number of arguments
Returns:
-
number
a count
See also:
- containsKeys (t, other)
-
Checks if all the keys of
other
table exists in tablet
. It does not compares values. The test is not commutative, i.e tablet
may contains keys not existing inother
.Parameters:
Returns:
-
boolean
true
orfalse
See also:
- sameKeys (tA, tB)
-
Checks if both given tables have the same keys. It does not compares values.
Parameters:
Returns:
-
boolean
true
orfalse
See also:
Array functions
- toArray ([...])
-
Converts a vararg list to an array-list.
Parameters:
- ... vararg Optional variable number of arguments
Returns:
-
table
an array-list of all passed-in args
- find (array, value[, from])
-
Looks for the first occurrence of a given value in an array. Returns the value index if found.
Parameters:
- array table an array of values
- value value a value to search for
- from number the index from where to start the search. Defaults to 1.
Returns:
-
number or nil
the index of the value if found in the array,
nil
otherwise. - reverse (array)
-
Reverses values in a given array. The passed-in array should not be sparse.
Parameters:
- array table an array
Returns:
-
table
a copy of the given array, reversed
- selectWhile (array, f[, ...])
-
Collects values from a given array. The passed-in array should not be sparse.
This function collects values as long as they satisfy a given predicate.
Therefore, it returns on the first falsy test.
Aliased astakeWhile
Parameters:
- array table an array
- f
function
an iterator function prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
a new table containing all values collected
See also:
- dropWhile (array, f[, ...])
-
Collects values from a given array. The passed-in array should not be sparse.
This function collects values as long as they do not satisfy a given predicate.
Therefore it returns on the first true test.
Aliased asrejectWhile
Parameters:
- array table an array
- f
function
an iterator function prototyped as
f(key,value,...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
a new table containing all values collected
- sortedIndex (array, the[, comp[, sort]])
-
Returns the index at which a value should be inserted. This returned index is determined so
that it maintains the sort. If a comparison function is passed, it will be used to sort all
values.
Parameters:
- array table an array
- the value value to be inserted
- comp
function
an comparison function prototyped as
f(a, b)
, defaults to < operator. - sort boolean whether or not the passed-in array should be sorted
Returns:
-
number
the index at which the passed-in value should be inserted
- indexOf (array, the)
-
Returns the index of a given value in an array. If the passed-in value exists
more than once in the array, it will return the index of the first occurrence.
Parameters:
- array table an array
- the value value to search for
Returns:
-
number or nil
the index of the passed-in value
See also:
- lastIndexOf (array, the)
-
Returns the index of the last occurrence of a given value.
Parameters:
- array table an array
- the value value to search for
Returns:
-
number or nil
the index of the last occurrence of the passed-in value or nil
See also:
- addTop (array, ...)
-
Adds all passed-in values at the top of an array. The last arguments will bubble to the
top of the given array.
Parameters:
- array table an array
- ... vararg a variable number of arguments
Returns:
-
table
the passed-in array
See also:
- push (array, ...)
-
Pushes all passed-in values at the end of an array.
Parameters:
- array table an array
- ... vararg a variable number of arguments
Returns:
-
table
the passed-in array
See also:
- pop (array[, n])
-
Removes and returns the values at the top of a given array.
Aliased asshift
Parameters:
- array table an array
- n number the number of values to be popped. Defaults to 1.
Returns:
-
vararg
a vararg list of values popped from the array
See also:
- unshift (array[, n])
-
Removes and returns the values at the end of a given array.
Parameters:
- array table an array
- n number the number of values to be unshifted. Defaults to 1.
Returns:
-
vararg
a vararg list of values
See also:
- pull (array, ...)
-
Removes all provided values in a given array.
Aliased asremove
Parameters:
- array table an array
- ... vararg a variable number of values to be removed from the array
Returns:
-
table
the passed-in array
- removeRange (array[, start[, finish]])
-
Trims all values indexed within the range
[start, finish]
.
Aliased asrmRange
Parameters:
- array table an array
- start number the lower bound index, defaults to the first index in the array.
- finish number the upper bound index, defaults to the array length.
Returns:
-
table
the passed-in array
- chunk (array, f[, ...])
-
Chunks together consecutive values. Values are chunked on the basis of the return
value of a provided predicate
f(key, value, ...)
. Consecutive elements which return the same value are chunked together. Leaves the first argument untouched if it is not an array.Parameters:
- array table an array
- f
function
an iterator function prototyped as
f(key, value, ...)
- ...
vararg
Optional extra-args to be passed to function
f
Returns:
-
table
a table of chunks (arrays)
See also:
- slice (array[, start[, finish]])
-
Slices values indexed within
[start, finish]
range.
Aliased as_.sub
Parameters:
- array table an array
- start number the lower bound index, defaults to the first index in the array.
- finish number the upper bound index, defaults to the array length.
Returns:
-
table
a new array
- first (array[, n])
-
Returns the first N values in an array.
Aliased ashead
,take
Parameters:
- array table an array
- n number the number of values to be collected, defaults to 1.
Returns:
-
table
a new array
See also:
- initial (array[, n])
-
Returns all values in an array excluding the last N values.
Parameters:
- array table an array
- n number the number of values to be left, defaults to the array length.
Returns:
-
table
a new array
See also:
- last (array[, n])
-
Returns the last N values in an array.
Parameters:
- array table an array
- n number the number of values to be collected, defaults to the array length.
Returns:
-
table
a new array
See also:
- rest (array[, index])
-
Trims all values before index.
Aliased astail
Parameters:
- array table an array
- index number an index, defaults to 1
Returns:
-
table
a new array
See also:
- compact (array)
-
Trims all falsy (false and nil) values.
Parameters:
- array table an array
Returns:
-
table
a new array
- flatten (array[, shallow])
-
Flattens a nested array. Passing
shallow
will only flatten at the first level.Parameters:
- array table an array
- shallow boolean specifies the flattening depth
Returns:
-
table
a new array, flattened
- difference (array, another)
-
Returns values from an array not present in all passed-in args.
Aliased aswithout
anddiff
Parameters:
Returns:
-
table
a new array
See also:
- union (...)
-
Returns the duplicate-free union of all passed in arrays.
Parameters:
- ... vararg a variable number of arrays arguments
Returns:
-
table
a new array
See also:
- intersection (array, ...)
-
Returns the intersection of all passed-in arrays.
Each value in the result is present in each of the passed-in arrays.
Parameters:
- array table an array
- ... vararg a variable number of array arguments
Returns:
-
table
a new array
See also:
- symmetricDifference (array, array2)
-
Performs a symmetric difference. Returns values from
array
not present inarray2
and also values fromarray2
not present inarray
.
Aliased assymdiff
Parameters:
Returns:
-
table
a new array
See also:
- unique (array)
-
Produces a duplicate-free version of a given array.
Aliased asuniq
Parameters:
- array table an array
Returns:
-
table
a new array, duplicate-free
See also:
- isunique (array)
-
Checks if a given array contains distinct values. Such an array is made of distinct elements,
which only occur once in this array.
Aliased asisuniq
Parameters:
- array table an array
Returns:
-
boolean
true
if the given array is unique,false
otherwise.See also:
- zip (...)
-
Merges values of each of the passed-in arrays in subsets.
Only values indexed with the same key in the given arrays are merged in the same subset.
Parameters:
- ... vararg a variable number of array arguments
Returns:
-
table
a new array
- append (array, other)
-
Clones
array
and appendsother
values.Parameters:
Returns:
-
table
a new array
- interleave (...)
-
Interleaves arrays. It returns a single array made of values from all
passed in arrays in their given order, interleaved.
Parameters:
- ... vararg a variable list of arrays
Returns:
-
table
a new array
See also:
- interpose (value, array)
-
Interposes
value
in-between consecutive pair of values inarray
.Parameters:
- value value a value
- array table an array
Returns:
-
table
a new array
See also:
- range ([from[, to[, step]]])
-
Produce a flexible list of numbers. If one positive value is passed, will count from 0 to that value,
with a default step of 1. If two values are passed, will count from the first one to the second one, with the
same default step of 1. A third passed value will be considered a step value.
Parameters:
- from number the initial value of the range
- to number the final value of the range
- step number the count step value
Returns:
-
table
a new array of numbers
- rep (value, n)
-
Creates an array list of
n
values, repeated.Parameters:
- value value a value to be repeated
- n
number
the number of repetitions of the given
value
.
Returns:
-
table
a new array of
n
values - partition. (array[, n])
-
Iterator returning partitions of an array. It returns arrays of length
n
made of values from the given array. In case the array size is not a multiple ofn
, the last array returned will be made of the rest of the values.Parameters:
- array table an array
- n number the size of each partition. Defaults to 1.
Returns:
-
function
an iterator function
- permutation (array)
-
Iterator returning the permutations of an array. It returns arrays made of all values
from the passed-in array, with values permuted.
Parameters:
- array table an array
Returns:
-
function
an iterator function
- invert (array)
-
Swaps keys with values. Produces a new array where previous keys are now values,
while previous values are now keys.
Aliased asmirror
Parameters:
- array table a given array
Returns:
-
table
a new array
- concat (array[, sep[, i[, j]]])
-
Concatenates values in a given array. Handles booleans as well. If
sep
string is passed, it will be used as a separator. Passingi
andj
will result in concatenating values within[i,j]
range.
Aliased asjoin
Parameters:
- array table a given array
- sep
string
a separator string, defaults to
''
. - i number the starting index, defaults to 1.
- j number the final index, defaults to the array length.
Returns:
-
string
a string
Utility functions
- identity (value)
-
Returns the passed-in value. This function seems useless, but it is used internally
as a default iterator.
Parameters:
- value value a value
Returns:
-
value
the passed-in value
- once (f)
-
Returns a version of
f
that runs only once. Successive calls tof
will keep yielding the same output, no matter what the passed-in arguments are. It can be used to initialize variables.Parameters:
- f function a function
Returns:
-
function
a new function
See also:
- memoize (f[, hash])
-
Memoizes a given function by caching the computed result.
Useful for speeding-up slow-running functions. If function
hash
is passed, it will be used to compute hash keys for a set of input values to the function for caching.
Aliased ascache
Parameters:
- f function a function
- hash function a hash function, defaults to identity
Returns:
-
function
a new function
- after (f, count)
-
Returns a version of
f
that runs on thecount-th
call. Useful when dealing with asynchronous tasks.Parameters:
- f function a function
- count
number
the number of calls before
f
answers
Returns:
-
function
a new function
See also:
- compose (...)
-
Composes functions. Each passed-in function consumes the return value of the function that follows.
In math terms, composing the functions
f
,g
, andh
produces the functionf(g(h(...)))
.Parameters:
- ... vararg a variable number of functions
Returns:
-
function
a new function
See also:
- pipe (value, ...)
-
Pipes a value through a series of functions. In math terms,
given some functions
f
,g
, andh
in that order, it returnsf(g(h(value)))
.Parameters:
- value value a value
- ... vararg a variable number of functions
Returns:
-
value
the result of the composition of function calls.
See also:
- complement (f)
-
Returns the logical complement of a given function. For a given input, the returned
function will output
false
if the original function would have returnedtrue
, and vice-versa.Parameters:
- f function a function
Returns:
-
function
the logical complement of the given function
f
. - juxtapose (value, ...)
-
Calls a sequence of passed-in functions with the same argument.
Returns a sequence of results.
Aliased asjuxt
Parameters:
- value value a value
- ... vararg a variable number of functions
Returns:
-
vararg
a vargarg list of results.
- wrap (f, wrapper)
-
Wraps
f
inside of thewrapper
function. It passesf
as the first argument towrapper
. This allows the wrapper to execute code before and afterf
runs, adjust the arguments, and execute it conditionally.Parameters:
- f
function
a function to be wrapped, prototyped as
f(...)
- wrapper
function
a wrapper function, prototyped as
wrapper(f,...)
Returns:
-
function
a new function
- f
function
a function to be wrapped, prototyped as
- times (n, iter, ...)
-
Runs
iter
functionn
times. Collects the results of each run and returns them in an array.Parameters:
- n
number
the number of times
iter
should be called - iter
function
an iterator function, prototyped as
iter(i, ...)
- ...
vararg
extra-args to be passed to
iter
function
Returns:
-
table
an array of results
- n
number
the number of times
- bind (f, v)
-
Binds
v
to be the first argument to functionf
. As a result, callingf(...)
will result tof(v, ...)
.Parameters:
- f function a function
- v value a value
Returns:
-
function
a function
See also:
- bindn (f, ...)
-
Binds
...
to be the N-first arguments to functionf
. As a result, callingf(a1, a2, ..., aN)
will result tof(..., a1, a2, ...,aN)
.Parameters:
- f function a function
- ... vararg a variable number of arguments
Returns:
-
function
a function
See also:
- uniqueId ([template[, ...]])
-
Generates a unique ID for the current session. If given a string template
will use this template for output formatting. Otherwise, if template is a function,
will evaluate
template(id, ...)
.
Aliased asuid
.Parameters:
- template string or function either a string or a function template to format the ID
- ... vararg a variable number of arguments to be passed to template, in case it is a function.
Returns:
-
value
an ID
Object functions
- keys (obj)
-
Returns the keys of the object properties.
Parameters:
- obj table an object
Returns:
-
table
an array
- values (obj)
-
Returns the values of the object properties.
Parameters:
- obj table an object
Returns:
-
table
an array
- toBoolean (value)
-
Converts any given value to a boolean
Parameters:
- value value a value. Can be of any type
Returns:
-
boolean
true
if value is true,false
otherwise (false or nil). - extend (destObj, ...)
-
Extends an object properties. It copies all of the properties of extra passed-in objects
into the destination object, and returns the destination object.
The last object in the
...
set will override properties of the same name in the previous oneParameters:
- destObj table a destination object
- ... vararg a variable number of array arguments
Returns:
-
table
the destination object extended
- functions ([obj])
-
Returns a sorted list of all methods names found in an object. If the given object
has a metatable implementing an
__index
field pointing to another table, will also recurse on this table if argumentrecurseMt
is provided. Ifobj
is omitted, it defaults to the library functions.
Aliased asmethods
.Parameters:
- obj table an object. Defaults to library functions.
Returns:
-
table
an array-list of methods names
- clone (obj[, shallow])
-
Clones a given object properties. If
shallow
is passed will also clone nested array properties.Parameters:
- obj table an object
- shallow boolean whether or not nested array-properties should be cloned, defaults to false.
Returns:
-
table
a copy of the passed-in object
- tap (obj, f[, ...])
-
Invokes interceptor with the object, and then returns object.
The primary purpose of this method is to "tap into" a method chain, in order to perform operations
on intermediate results within the chain.
Parameters:
- obj table an object
- f
function
an interceptor function, should be prototyped as
f(obj, ...)
- ... vararg Extra-args to be passed to interceptor function
Returns:
-
table
the passed-in object
- has (obj, key)
-
Checks if a given object implements a property.
Parameters:
- obj table an object
- key value a key property to be checked
Returns:
-
boolean
true
orfalse
- pick (obj, ...)
-
Return a filtered copy of the object. The returned object will only have
the white-listed properties paired with their original values.
Aliased aschoose
.Parameters:
- obj table an object
- ... vararg a variable number of string keys
Returns:
-
table
the filtered object
- omit (obj, ...)
-
Return a filtered copy of the object. The returned object will not have
the black-listed properties.
Aliased asdrop
.Parameters:
- obj table an object
- ... vararg a variable number of string keys
Returns:
-
table
the filtered object
- template (obj[, template])
-
Fills nil properties in an object with the given template object. Pre-existing
properties will be preserved.
Aliased asdefaults
.Parameters:
Returns:
-
table
the passed-in object filled
- isEqual (objA, objB[, useMt])
-
Performs a deep comparison test between two objects. Can compare strings, functions
(by reference), nil, booleans. Compares tables by reference or by values. If
useMt
is passed, the equality operator==
will be used if one of the given objects has a metatable implementing__eq
.
Aliased as_.compare
Parameters:
- objA table an object
- objB table another object
- useMt
boolean
whether or not
__eq
should be used, defaults to false.
Returns:
-
boolean
true
orfalse
- result (obj, method[, ...])
-
Invokes an object method. It passes the object itself as the first argument. if
method
is not callable, will returnobj[method]
.Parameters:
- obj table an object
- method
string
a string key to index in object
obj
. - ...
vararg
Optional extra-args to be passed to
method
Returns:
-
value
the returned value of
method(obj,...)
call - isTable (t)
-
Checks if the given arg is a table.
Parameters:
- t table a value to be tested
Returns:
-
boolean
true
orfalse
- isCallable (obj)
-
Checks if the given argument is an callable. Assumes
obj
is callable if it is either a function or a table having a metatable implementing__call
metamethod.Parameters:
- obj table an object
Returns:
-
boolean
true
orfalse
- isArray (obj)
-
Checks if the given argument is an array. Assumes
obj
is an array if is a table with integer numbers starting at 1.Parameters:
- obj table an object
Returns:
-
boolean
true
orfalse
- isIterable (obj)
-
Checks if the given object is iterable with pairs (or ipairs ).
Parameters:
- obj table an object
Returns:
-
boolean
true
if the object can be iterated with pairs ,false
otherwise - isEmpty ([obj])
-
Checks if the given is empty. If
obj
is a string, will returntrue
if#obj == 0
. Otherwise, ifobj
is a table, will return whether or not this table is empty. Ifobj
isnil
, it will return true.Parameters:
Returns:
-
boolean
true
orfalse
- isString (obj)
-
Checks if the given argument is a string.
Parameters:
- obj table an object
Returns:
-
boolean
true
orfalse
- isFunction (obj)
-
Checks if the given argument is a function.
Parameters:
- obj table an object
Returns:
-
boolean
true
orfalse
- isNil (obj)
-
Checks if the given argument is nil.
Parameters:
- obj table an object
Returns:
-
boolean
true
orfalse
- isNumber (obj)
-
Checks if the given argument is a number.
Parameters:
- obj table a number
Returns:
-
boolean
true
orfalse
See also:
- isNaN (obj)
-
Checks if the given argument is NaN (see Not-A-Number).
Parameters:
- obj table a number
Returns:
-
boolean
true
orfalse
See also:
- isFinite (obj)
-
Checks if the given argument is a finite number.
Parameters:
- obj table a number
Returns:
-
boolean
true
orfalse
- isBoolean (obj)
-
Checks if the given argument is a boolean.
Parameters:
- obj table a boolean
Returns:
-
boolean
true
orfalse
- isInteger (obj)
-
Checks if the given argument is an integer.
Parameters:
- obj table a number
Returns:
-
boolean
true
orfalse
- chain (value)
-
Returns a wrapped object. Calling library functions as methods on this object
will continue to return wrapped objects until obj:value is used. Can be aliased as
_(value)
.Parameters:
- value value a value to be wrapped
Returns:
-
object
a wrapped object
- obj:value ()
-
Extracts the value of a wrapped object. Must be called on an chained object (see chain).
Returns:
-
value
the value previously wrapped
- import ([context[, noConflict]])
-
Imports all library functions into a context.
Parameters:
- context
table
a context. Defaults to
_G
(global environment) when not given. - noConflict boolean Skips function import in case its key exists in the given context
Returns:
-
table
the passed-in context
- context
table
a context. Defaults to