Module moses
Utility-belt library for functional programming in Lua (source)
Info:
- Copyright: 2012-2017
- Release: 1.6.1
- License: MIT
- Author: Roland Yonaba
Table functions
clear (t) | Clears a table. |
each (t, f[, ...]) | Iterates on key-value pairs, calling f (k, v) at every step. |
eachi (t, f[, ...]) | Iterates on integer key-value pairs, calling f(k, v) every step. |
at (t, ...) | Collects values at given keys and return them wrapped in an array. |
count (t[, value]) | Counts occurrences of a given value in a table. |
countf (t, f[, ...]) | Counts occurrences validating a predicate. |
cycle (t, n) | Loops n times through a table. |
map (t, f[, ...]) | Maps f (k, v) on key-value pairs, collects and returns the results. |
reduce (t, f[, state]) | Reduces a table, left-to-right. |
reduceby (t, f, state, pred[, ...]) | Reduces values in a table passing a given predicate. |
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) | Performs a linear search for a value in a table. |
detect (t, value) | Performs a linear search for a value in a table. |
where (t, props) | Returns all values having specified keys props . |
findWhere (t, props) | Returns the first value having specified keys props . |
select (t, f[, ...]) | Selects and returns 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, key) | Extracts values in a table having a given key. |
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. |
sortBy (t[, transform[, comp]]) | Sorts a table in-place using a transform. |
groupBy (t, iter[, ...]) | Splits a table into subsets groups. |
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
sample (array[, n[, seed]]) | Samples n random values from an array. |
sampleProb (array, prob[, seed]) | Return elements from a sequence with a given probability. |
toArray (...) | Converts a list of arguments to an array. |
find (array, value[, from]) | Looks for the first occurrence of a given value in an array. |
reverse (array) | Returns an array where values are in reverse order. |
fill (array, value[, i[, j]]) | Replaces elements in a given array with a given value. |
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, value) | Returns the index of the first occurence of value in an array. |
lastIndexOf (array, value) | Returns the index of the last occurrence of value in an array. |
findIndex (array, predicate[, ...]) | Returns the first index at which a predicate returns true. |
findLastIndex (array, predicate[, ...]) | Returns the last index at which a predicate returns true. |
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]]) | Removes values at index 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]) | Removes all values before index. |
nth (array, index) | Returns the value at a given index. |
compact (array) | Removes 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]]]) | Produces a flexible list of numbers. |
rep (value, n) | Creates an array list of n values, repeated. |
partition (array[, n[, pad]]) | Iterator returning partitions of an array. |
sliding. (array[, n[, pad]]) | Iterator returning sliding 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
noop () | The no-operation function. |
identity (value) | Returns the passed-in value. |
constant (value) | Creates a constant function which returns the same output on every call. |
memoize (f[, hash]) | Memoizes a given function by caching the computed result. |
once (f) | Returns a version of f that runs only once. |
before (f, count) | Returns a version of f that will run no more than count times. |
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 f . |
bind2 (f, v) | Binds v to be the second argument to f . |
bindn (f, ...) | Binds … to be the N-first arguments to function f . |
bindAll (obj, ...) | Binds methods to object. |
uniqueId ([template[, ...]]) | Generates an unique ID for the current session. |
iterator (f, x) | Produces an iterator which repeatedly apply a function f onto an input. |
array (...) | Iterates an iterator and returns its values in an array. |
flip (f) | Creates a function of f with arguments flipped in reverse order. |
over (...) | Creates a function that runs transforms on all arguments it receives. |
overEvery (...) | Creates a validation function. |
overSome (...) | Creates a validation function. |
overArgs (f, ...) | Creates a function that invokes f with its arguments transformed. |
partial (f, ...) | Partially apply a function by filling in any number of its arguments. |
partialRight (f, ...) | Similar to partial, but from the right. |
curry (f[, n_args]) | Curries a function. |
time (f[, ...]) | Returns the execution time of f (…) and its returned values. |
Object functions
keys (obj) | Returns the keys of the object properties. |
values (obj) | Returns the values of the object properties. |
kvpairs (obj) | Converts keys and values a an array-list of [k, v]. |
toObj (kvpairs) | Converts an array list of kvpairs to an object. |
property (key) | Returns a function that will return the key property of any passed-in object. |
propertyOf (obj) | Returns a function which will return the value of an object property. |
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, ...) | Returns an object copy having white-listed properties. |
omit (obj, ...) | Returns an object copy without black-listed properties. |
template (obj[, template]) | Applies a template to an object, preserving non-nil properties. |
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 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 pbject 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
- clear (t)
-
Clears a table. All its values become nil.
Parameters:
- t a table
Returns:
-
the given table, cleared.
- each (t, f[, ...])
-
Iterates on key-value pairs, calling
f (k, v)
at every step.
Aliased asforEach
.Parameters:
- t a table
- f
a function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
See also:
- eachi (t, f[, ...])
-
Iterates on integer key-value pairs, calling
f(k, v)
every step.
Only applies to values located at integer keys. The table can be a sparse array. Iteration will start from the lowest integer key found to the highest one.
Aliased asforEachi
.Parameters:
- t a table
- f
a function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
See also:
- at (t, ...)
-
Collects values at given keys and return them wrapped in an array.
Parameters:
- t a table
- ... A variable number of keys to collect values
Returns:
-
an array-list of values
- count (t[, value])
-
Counts occurrences of a given value in a table. Uses isEqual to compare values.
Parameters:
- t a table
- value a value to be searched in the table. If not given, the size of the table will be returned (optional)
Returns:
-
the count of occurrences of the given 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 (k, v, …)
Parameters:
- t a table
- f
an iterator function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
the count of values validating the predicate
See also:
- cycle (t, n)
-
Loops
n
times through a table. In casen
is omitted, it will loop forever. In casen
is lower or equal to 0, it returns an empty function.
Aliased asloop
.Parameters:
- t a table
- n the number of loops
Returns:
-
an iterator function yielding key-value pairs from the passed-in table.
- map (t, f[, ...])
-
Maps
f (k, v)
on key-value pairs, collects and returns the results.
Aliased ascollect
.Parameters:
- t a table
- f
an iterator function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
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 a single value, using a given iterator and an initial state.
The iterator takes a state and a value and returns a new state.
Aliased asinject
,foldl
.Parameters:
- t a table
- f
an iterator function, prototyped as
f (state, value)
- state an initial state of reduction. Defaults to the first value in the table. (optional)
Returns:
-
the final state of reduction
See also:
- reduceby (t, f, state, pred[, ...])
-
Reduces values in a table passing a given predicate. Folds the table left-to-right, considering
only values validating a given predicate.
Parameters:
- t a table
- f
an iterator function, prototyped as
f (state, value)
- state an initial state of reduction.
- pred
a predicate function
pred (k, v, …)
to select values to be considered for reduction - ...
optional args to be passed to
pred
(optional)
Returns:
-
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, using a given iterator and an initial state.
The iterator takes a state and a value, and returns a new state.
Aliased asinjectr
,foldr
.Parameters:
- t a table
- f
an iterator function, prototyped as
f (state, value)
- state an initial state of reduction. Defaults to the last value in the table. (optional)
Returns:
-
the final state of reduction
See also:
- mapReduce (t, f[, state])
-
Reduces a table while saving intermediate states. Folds the table left-to-right
using a given iterator and an initial state. The iterator takes a state and a value,
and returns a new state. The result is an array of intermediate states.
Aliased asmapr
Parameters:
- t a table
- f
an iterator function, prototyped as
f (state, value)
- state an initial state of reduction. Defaults to the first value in the table. (optional)
Returns:
-
an array of states
See also:
- mapReduceRight (t, f[, state])
-
Reduces a table while saving intermediate states. Folds the table right-to-left
using a given iterator and an initial state. The iterator takes a state and a value,
and returns a new state. The result is an array of intermediate states.
Aliased asmaprr
Parameters:
- t a table
- f
an iterator function, prototyped as
f (state, value)
- state an initial state of reduction. Defaults to the last value in the table. (optional)
Returns:
-
an array of states
See also:
- include (t, value)
-
Performs a linear search for a value in a table. It does not work for nested tables.
The given value can be a function prototyped as
f (v, value)
which should return true when any v in the table equals the value being searched.
Aliased asany
,some
,contains
Parameters:
- t a table
- value a value to search for
Returns:
-
a boolean :
true
when found,false
otherwiseSee also:
- detect (t, value)
-
Performs a linear search for a value in a table. Returns the key of the value if found.
The given value can be a function prototyped as
f (v, value)
which should return true when any v in the table equals the value being searched.Parameters:
- t a table
- value a value to search for
Returns:
-
the key of the value when found or nil
See also:
- where (t, props)
-
Returns all values having specified keys
props
.Parameters:
- t a table
- props a set of keys
Returns:
-
an array of values from the passed-in table
See also:
- findWhere (t, props)
-
Returns the first value having specified keys
props
.Parameters:
- t a table
- props a set of keys
Returns:
-
a value from the passed-in table
See also:
- select (t, f[, ...])
-
Selects and returns values passing an iterator test.
Aliased asfilter
.Parameters:
- t a table
- f
an iterator function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
the selected values
See also:
- reject (t, f[, ...])
-
Clones a table while dropping values passing an iterator test.
Aliased asdiscard
Parameters:
- t a table
- f
an iterator function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
the remaining values
See also:
- all (t, f[, ...])
-
Checks if all values in a table are passing an iterator test.
Aliased asevery
Parameters:
- t a table
- f
an iterator function, prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
true
if all values passes the predicate,false
otherwise - invoke (t, method[, ...])
-
Invokes a method on each value in a table.
Parameters:
- t a table
- method
a function, prototyped as
f (v, …)
- ...
Optional args to be passed to
method
(optional)
Returns:
-
the result of the call
f (v, …)
See also:
- pluck (t, key)
-
Extracts values in a table having a given key.
Parameters:
- t a table
- key
a key, will be used to index in each value:
value[key]
Returns:
-
an array of values having the given key
- max (t[, transform[, ...]])
-
Returns the max value in a collection. If an transformation function is passed, it will
be used to evaluate values by which all objects will be sorted.
Parameters:
- t a table
- transform
a transformation function, prototyped as
transform (v, …)
, defaults to identity (optional) - ...
Optional args to be passed to
transform
(optional)
Returns:
-
the max 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 evaluate values by which all objects will be sorted.
Parameters:
- t a table
- transform
a transformation function, prototyped as
transform (v, …)
, defaults to identity (optional) - ...
Optional args to be passed to
transform
(optional)
Returns:
-
the min 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 pseudo random number generator (using math.randomseed).
Parameters:
- t a table
- seed a seed (optional)
Returns:
-
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:
- a a table
- b another table
Returns:
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 a table
- comp
a comparison function prototyped as
comp (a, b)
, defaults to < operator. (optional)
Returns:
-
the initial table, sorted.
See also:
- sortBy (t[, transform[, comp]])
-
Sorts a table in-place using a transform. Values are ranked in a custom order of the results of
running
transform (v)
on all values.transform
may also be a string name property sort by.comp
is a comparison function.Parameters:
- t a table
- transform
a
transform
function to sort elements prototyped astransform (v)
. Defaults to identity (optional) - comp
a comparision function, defaults to the
<
operator (optional)
Returns:
-
a new array of sorted values
See also:
- groupBy (t, iter[, ...])
-
Splits a table into subsets groups.
Parameters:
- t a table
- iter
an iterator function, prototyped as
iter (k, v, …)
- ...
Optional args to be passed to
iter
(optional)
Returns:
-
a table of subsets groups
- countBy (t, iter[, ...])
-
Groups values in a collection and counts them.
Parameters:
- t a table
- iter
an iterator function, prototyped as
iter (k, v, …)
- ...
Optional args to be passed to
iter
(optional)
Returns:
-
a table of subsets groups names paired with their count
- size ([...])
-
Counts the number of values in a collection. If being passed more than one argument
it will return the count of all passed-in arguments.
Parameters:
- ... Optional variable number of arguments (optional)
Returns:
-
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:
- t a table
- other another table
Returns:
true
orfalse
See also:
- sameKeys (tA, tB)
-
Checks if both given tables have the same keys. It does not compares values.
Parameters:
- tA a table
- tB another table
Returns:
true
orfalse
See also:
Array functions
- sample (array[, n[, seed]])
-
Samples
n
random values from an array. Ifn
is not specified, returns a single element. It uses internally shuffle to shuffle the array before sampling values. Ifseed
is passed, it will be used for shuffling.Parameters:
- array an array
- n a number of elements to be sampled. Defaults to 1. (optional)
- seed an optional seed for shuffling (optional)
Returns:
-
an array of selected values or a single value when
n
== 1See also:
- sampleProb (array, prob[, seed])
-
Return elements from a sequence with a given probability. It considers each value independently.
Providing a seed will result in deterministic sampling. Given the same seed it will return the same sample
every time.
Parameters:
- array an array
- prob a probability for each element in array to be selected
- seed an optional seed for deterministic sampling (optional)
Returns:
-
an array of selected values
See also:
- toArray (...)
-
Converts a list of arguments to an array.
Parameters:
- ... a list of arguments
Returns:
-
an array 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.
Uses isEqual to compare values.
Parameters:
- array an array of values
- value a value to lookup for
- from the index from where the search will start. Defaults to 1. (optional)
Returns:
-
the index of the value if found in the array,
nil
otherwise. - reverse (array)
-
Returns an array where values are in reverse order. The passed-in array should not be sparse.
Parameters:
- array an array
Returns:
-
a reversed array
- fill (array, value[, i[, j]])
-
Replaces elements in a given array with a given value. In case
i
andj
are given it will only replaces values at indexes between[i,j]
. In casej
is greather than the array size, it will append new values, increasing the array.Parameters:
- array an array
- value a value
- i the index from which to start replacing values. Defaults to 1. (optional)
- j the index where to stop replacing values. Defaults to the array size. (optional)
Returns:
-
the original array with values changed
- 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 and returns on the first falsy test.
Aliased astakeWhile
Parameters:
- array an array
- f
an iterator function prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
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 and returns on the first truthy test.
Aliased asrejectWhile
Parameters:
- array an array
- f
an iterator function prototyped as
f (k,v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
a new table containing all values collected
- sortedIndex (array, the[, comp[, sort]])
-
Returns the index at which a value should be inserted. This index is evaluated so
that it maintains the sort. If a comparison function is passed, it will be used to sort
values.
Parameters:
- array an array
- the value to be inserted
- comp
an comparison function prototyped as
f (a, b)
, defaults to < operator. (optional) - sort whether or not the passed-in array should be sorted (optional)
Returns:
-
number the index at which the passed-in value should be inserted
- indexOf (array, value)
-
Returns the index of the first occurence of value in an array.
Parameters:
- array an array
- value the value to search for
Returns:
-
the index of the passed-in value
See also:
- lastIndexOf (array, value)
-
Returns the index of the last occurrence of value in an array.
Parameters:
- array an array
- value the value to search for
Returns:
-
the index of the last occurrence of the passed-in value or nil
See also:
- findIndex (array, predicate[, ...])
-
Returns the first index at which a predicate returns true.
Parameters:
- array an array
- predicate
a predicate function prototyped as
predicate (k, v, …)
- ...
optional arguments to
pred
(optional)
Returns:
-
the index found or nil
See also:
- findLastIndex (array, predicate[, ...])
-
Returns the last index at which a predicate returns true.
Parameters:
- array an array
- predicate
a predicate function prototyped as
predicate (k, v, …)
- ...
optional arguments to
pred
(optional)
Returns:
-
the index found or nil
See also:
- addTop (array, ...)
-
Adds all passed-in values at the top of an array. The last elements will bubble to the
top of the given array.
Parameters:
- array an array
- ... a variable number of arguments
Returns:
-
the passed-in array with new values added
See also:
- push (array, ...)
-
Pushes all passed-in values at the end of an array.
Parameters:
- array an array
- ... a variable number of arguments
Returns:
-
the passed-in array with new added values
See also:
- pop (array[, n])
-
Removes and returns the values at the top of a given array.
Aliased asshift
Parameters:
- array an array
- n the number of values to be popped. Defaults to 1. (optional)
Returns:
-
the popped values
See also:
- unshift (array[, n])
-
Removes and returns the values at the end of a given array.
Parameters:
- array an array
- n the number of values to be unshifted. Defaults to 1. (optional)
Returns:
-
the values
See also:
- pull (array, ...)
-
Removes all provided values in a given array.
Aliased asremove
Parameters:
- array an array
- ... a variable number of values to be removed from the array
Returns:
-
the passed-in array with values removed
- removeRange (array[, start[, finish]])
-
Removes values at index within the range
[start, finish]
.
Aliased asrmRange
,chop
Parameters:
- array an array
- start the lower bound index, defaults to the first index in the array. (optional)
- finish the upper bound index, defaults to the array length. (optional)
Returns:
-
the passed-in array with values removed
- chunk (array, f[, ...])
-
Chunks together consecutive values. Values are chunked on the basis of the return
value of a provided predicate
f (k, v, …)
. Consecutive elements which return the same value are chunked together. Leaves the first argument untouched if it is not an array.Parameters:
- array an array
- f
an iterator function prototyped as
f (k, v, …)
- ...
Optional args to be passed to
f
(optional)
Returns:
-
a table of chunks (arrays)
See also:
- slice (array[, start[, finish]])
-
Slices values indexed within
[start, finish]
range.
Aliased as_.sub
Parameters:
- array an array
- start the lower bound index, defaults to the first index in the array. (optional)
- finish the upper bound index, defaults to the array length. (optional)
Returns:
-
a new array of sliced values
- first (array[, n])
-
Returns the first N values in an array.
Aliased ashead
,take
Parameters:
- array an array
- n the number of values to be collected, defaults to 1. (optional)
Returns:
-
a new array
See also:
- initial (array[, n])
-
Returns all values in an array excluding the last N values.
Parameters:
- array an array
- n the number of values to be left, defaults to the array length. (optional)
Returns:
-
a new array
See also:
- last (array[, n])
-
Returns the last N values in an array.
Parameters:
- array an array
- n the number of values to be collected, defaults to the array length. (optional)
Returns:
-
a new array
See also:
- rest (array[, index])
-
Removes all values before index.
Aliased astail
Parameters:
- array an array
- index an index, defaults to 1 (optional)
Returns:
-
a new array
See also:
- nth (array, index)
-
Returns the value at a given index.
Parameters:
- array an array
- index an index
Returns:
-
the value at the given index
- compact (array)
-
Removes all falsy (false and nil) values.
Parameters:
- array an array
Returns:
-
a new array
- flatten (array[, shallow])
-
Flattens a nested array. Passing
shallow
will only flatten at the first level.Parameters:
- array an array
- shallow specifies the flattening depth (optional)
Returns:
-
a new array, flattened
- difference (array, another)
-
Returns values from an array not present in all passed-in args.
Aliased aswithout
anddiff
Parameters:
- array an array
- another array
Returns:
-
a new array
See also:
- union (...)
-
Returns the duplicate-free union of all passed in arrays.
Parameters:
- ... a variable number of arrays arguments
Returns:
-
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 an array
- ... a variable number of array arguments
Returns:
-
a new array
See also:
- symmetricDifference (array, array2)
-
Performs a symmetric difference. Returns values from array not present in
array2
and also values fromarray2
not present in array.
Aliased assymdiff
Parameters:
- array an array
- array2 another array
Returns:
-
a new array
See also:
- unique (array)
-
Produces a duplicate-free version of a given array.
Aliased asuniq
Parameters:
- array an array
Returns:
-
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 an array
Returns:
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.
Aliased astranspose
Parameters:
- ... a variable number of array arguments
Returns:
-
a new array
- append (array, other)
-
Clones array and appends
other
values.Parameters:
- array an array
- other an array
Returns:
-
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:
- ... a variable list of arrays
Returns:
-
a new array
See also:
- interpose (value, array)
-
Interposes value in-between consecutive pair of values in array.
Parameters:
- value a value
- array an array
Returns:
-
a new array
See also:
- range ([from[, to[, step]]])
-
Produces 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 value passed will be considered a step value.
Parameters:
- from the initial value of the range (optional)
- to the final value of the range (optional)
- step the step of count (optional)
Returns:
-
a new array of numbers
- rep (value, n)
-
Creates an array list of
n
values, repeated.Parameters:
- value a value to be repeated
- n the number of repetitions of value.
Returns:
-
a new array of
n
values - partition (array[, n[, pad]])
-
Iterator returning partitions of an array. It returns arrays of length
n
made of values from the given array. If the last partition has lower elements thann
andpad
is supplied, it will be adjusted ton
of elements withpad
value.Parameters:
- array an array
- n the size of partitions. Should be greater than 0. Defaults to 1. (optional)
- pad
a value to adjust the last subsequence to the
n
elements (optional)
Returns:
-
an iterator function
- sliding. (array[, n[, pad]])
-
Iterator returning sliding partitions of an array. It returns overlapping subsequences
of length
n
. If the last subsequence has lower elements thann
andpad
is supplied, it will be adjusted ton
elements withpad
value.Parameters:
- array an array
- n the size of partitions. Should be greater than 1. Defaults to 2. (optional)
- pad
a value to adjust the last subsequence to the
n
elements (optional)
Returns:
-
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 an array
Returns:
-
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 a given array
Returns:
-
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 only values within[i, j]
range.
Aliased asjoin
Parameters:
- array a given array
- sep
a separator string, defaults to the empty string
''
. (optional) - i the starting index, defaults to 1. (optional)
- j the final index, defaults to the array length. (optional)
Returns:
-
a string
Utility functions
- noop ()
-
The no-operation function.
Returns:
-
nothing
- identity (value)
-
Returns the passed-in value. This function is used internally
as a default iterator.
Parameters:
- value a value
Returns:
-
the passed-in value
- constant (value)
-
Creates a constant function which returns the same output on every call.
Parameters:
- value a constant value
Returns:
-
a constant function
- memoize (f[, hash])
-
Memoizes a given function by caching the computed result.
Useful for speeding-up slow-running functions. If a
hash
function is passed, it will be used to compute hash keys for a set of input values for caching.
Aliased ascache
Parameters:
- f a function
- hash a hash function, defaults to identity (optional)
Returns:
-
a new function
- 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 a function
Returns:
-
a new function
See also:
- before (f, count)
-
Returns a version of
f
that will run no more than count times. Next calls will keep yielding the results of the count-th call.Parameters:
- f a function
- count a count
Returns:
-
a new function
See also:
- after (f, count)
-
Returns a version of
f
that runs on thecount-th
call. Useful when dealing with asynchronous tasks.Parameters:
- f a function
- count
the number of calls before
f
will start running.
Returns:
-
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:
- ... a variable number of functions
Returns:
-
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 a value
- ... a variable number of functions
Returns:
-
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 a function
Returns:
-
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 a value
- ... a variable number of functions
Returns:
-
a 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
a function to be wrapped, prototyped as
f (…)
- wrapper
a wrapper function, prototyped as
wrapper (f, …)
Returns:
-
the results
- f
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
the number of times
iter
should be called - iter
an iterator function, prototyped as
iter (i, …)
- ...
args to be passed to
iter
function
Returns:
-
table an array of results
- n
the number of times
- bind (f, v)
-
Binds
v
to be the first argument tof
. Callingf (…)
will result tof (v, …)
.Parameters:
- f a function
- v a value
Returns:
-
a function
See also:
- bind2 (f, v)
-
Binds
v
to be the second argument tof
. Callingf (a, …)
will result tof (a, v, …)
.Parameters:
- f a function
- v a value
Returns:
-
a function
See also:
- bindn (f, ...)
-
Binds
…
to be the N-first arguments to functionf
.
Callingf (a1, a2, …, aN)
will result tof (…, a1, a2, …,aN)
.Parameters:
- f a function
- ... a variable number of arguments
Returns:
-
a function
See also:
- bindAll (obj, ...)
-
Binds methods to object. As such, whenever any of these methods is invoked, it
always receives the object as its first argument.
Parameters:
- obj an abject
- ... a variable number of method names
Returns:
-
the passed-in object with all methods bound to the object itself.
See also:
- uniqueId ([template[, ...]])
-
Generates an unique ID for the current session. If given a string template, it
will use this template for output formatting. Otherwise, if template is a function, it
will evaluate
template (id, …)
.
Aliased asuid
.Parameters:
- template either a string or a function template to format the ID (optional)
- ... a variable number of arguments to be passed to template, in case it is a function. (optional)
Returns:
-
value an ID
- iterator (f, x)
-
Produces an iterator which repeatedly apply a function
f
onto an input.
Yields x, then f(x), then f(f(x)), continuously.Parameters:
- f a function
- x
an initial input to
f
Returns:
-
an iterator fnction
Aliased asiter
. - array (...)
-
Iterates an iterator and returns its values in an array.
Parameters:
- ... an iterator (a function, a table and a value)
Returns:
-
an array of results
- flip (f)
-
Creates a function of
f
with arguments flipped in reverse order.Parameters:
- f a function
Returns:
-
a function
- over (...)
-
Creates a function that runs transforms on all arguments it receives.
Parameters:
- ... a set of functions which will receive all arguments to the returned function
Returns:
-
a function
See also:
- overEvery (...)
-
Creates a validation function. The returned function checks if all of the given predicates return
truthy when invoked with the arguments it receives.
Parameters:
- ... a list of predicate functions
Returns:
-
a new function
See also:
- overSome (...)
-
Creates a validation function. The return function checks if any of a given predicates return
truthy when invoked with the arguments it receives.
Parameters:
- ... a list of predicate functions
Returns:
-
a new function
See also:
- overArgs (f, ...)
-
Creates a function that invokes
f
with its arguments transformed. 1rst arguments will be passed to the 1rst transform, 2nd arg to the 2nd transform, etc. Remaining arguments will not be transformed.Parameters:
- f a function
- ...
a list of transforms funcs prototyped as
f (v)
Returns:
-
the result of running
f
with its transformed argumentsSee also:
- partial (f, ...)
-
Partially apply a function by filling in any number of its arguments.
One may pass a string'_'
as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but left open to be supplied at call-time.Parameters:
- f a function
- ...
a list of partial arguments to
f
Returns:
-
a new version of function f having some of it original arguments filled
See also:
- partialRight (f, ...)
-
Similar to partial, but from the right.
Parameters:
- f a function
- ...
a list of partial arguments to
f
Returns:
-
a new version of function f having some of it original arguments filled
See also:
- curry (f[, n_args])
-
Curries a function. If the given function
f
takes multiple arguments, it returns another version off
that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result.Parameters:
- f a function
- n_args
the number of arguments expected for
f
. Defaults to 2. (optional)
Returns:
-
a curried version of
f
See also:
- time (f[, ...])
-
Returns the execution time of
f (…)
and its returned values.Parameters:
- f a function
- ...
optional args to
f
(optional)
Returns:
-
the execution time and the results of
f (…)
Object functions
- keys (obj)
-
Returns the keys of the object properties.
Parameters:
- obj an object
Returns:
-
an array
- values (obj)
-
Returns the values of the object properties.
Parameters:
- obj an object
Returns:
-
an array
- kvpairs (obj)
-
Converts keys and values a an array-list of [k, v].
Parameters:
- obj an object
Returns:
-
an array list of key-values pairs
See also:
- toObj (kvpairs)
-
Converts an array list of kvpairs to an object. Keys are taken
from the 1rst column in the kvpairs sequence, associated with values in the 2nd
column
Parameters:
- kvpairs an array-list of kvpairs
Returns:
-
an object
See also:
- property (key)
-
Returns a function that will return the key property of any passed-in object.
Parameters:
- key a key property name
Returns:
-
a function which should accept an object as argument
See also:
- propertyOf (obj)
-
Returns a function which will return the value of an object property.
Parameters:
- obj an object
Returns:
-
a function which should accept a key property argument
See also:
- toBoolean (value)
-
Converts any given value to a boolean
Parameters:
- value a value. Can be of any type
Returns:
true
if value is true,false
otherwise (false or nil). - extend (destObj, ...)
-
Extends an object properties. It copies the properties of extra passed-in objects
into the destination object, and returns the destination object. The last objects
will override properties of the same name.
Parameters:
- destObj a destination object
- ... a list of objects
Returns:
-
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 ifrecurseMt
is provided. Ifobj
is omitted, it defaults to the library functions.
Aliased asmethods
.Parameters:
- obj an object. Defaults to Moses library functions. (optional)
Returns:
-
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 an object
- shallow whether or not nested array-properties should be cloned, defaults to false. (optional)
Returns:
-
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 an object
- f
an interceptor function, should be prototyped as
f (obj, …)
- ...
args to be passed to
f
(optional)
Returns:
-
the passed-in object
- has (obj, key)
-
Checks if a given object implements a property.
Parameters:
- obj an object
- key a key property to be checked
Returns:
true
orfalse
- pick (obj, ...)
-
Returns an object copy having white-listed properties.
Aliased aschoose
.Parameters:
- obj an object
- ... a variable number of string keys
Returns:
-
the filtered object
- omit (obj, ...)
-
Returns an object copy without black-listed properties.
Aliased asdrop
.Parameters:
- obj an object
- ... a variable number of string keys
Returns:
-
the filtered object
- template (obj[, template])
-
Applies a template to an object, preserving non-nil properties.
Aliased asdefaults
.Parameters:
- obj an object
- template
a template object. Defaults to an empty table
{}
. (optional)
Returns:
-
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 an object
- objB another object
- useMt
whether or not
__eq
should be used, defaults to false. (optional)
Returns:
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 an object
- method
a string key to index in object
obj
. - ...
Optional args to be passed to
method
(optional)
Returns:
-
the returned value of
method (obj, …)
call - isTable (t)
-
Checks if the given arg is a table.
Parameters:
- t a value to be tested
Returns:
true
orfalse
- isCallable (obj)
-
Checks if the given argument is callable. Assumes
obj
is callable if it is either a function or a table having a metatable implementing__call
metamethod.Parameters:
- obj an object
Returns:
true
orfalse
- isArray (obj)
-
Checks if the given argument is an array. Assumes
obj
is an array if is a table with consecutive integer keys starting at 1.Parameters:
- obj an object
Returns:
true
orfalse
- isIterable (obj)
-
Checks if the given object is iterable with pairs (or ipairs).
Parameters:
- obj an object
Returns:
- isEmpty ([obj])
-
Checks if the given pbject 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:
- obj an object (optional)
Returns:
true
orfalse
- isString (obj)
-
Checks if the given argument is a string.
Parameters:
- obj an object
Returns:
true
orfalse
- isFunction (obj)
-
Checks if the given argument is a function.
Parameters:
- obj an object
Returns:
true
orfalse
- isNil (obj)
-
Checks if the given argument is nil.
Parameters:
- obj an object
Returns:
true
orfalse
- isNumber (obj)
-
Checks if the given argument is a number.
Parameters:
- obj an object
Returns:
true
orfalse
See also:
- isNaN (obj)
-
Checks if the given argument is NaN (see Not-A-Number).
Parameters:
- obj an object
Returns:
true
orfalse
See also:
- isFinite (obj)
-
Checks if the given argument is a finite number.
Parameters:
- obj an object
Returns:
true
orfalse
- isBoolean (obj)
-
Checks if the given argument is a boolean.
Parameters:
- obj an object
Returns:
true
orfalse
- isInteger (obj)
-
Checks if the given argument is an integer.
Parameters:
- obj an object
Returns:
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 a value to be wrapped
Returns:
-
a wrapped object
- obj:value ()
-
Extracts the value of a wrapped object. Must be called on an chained object (see chain).
Returns:
-
the value previously wrapped
- import ([context[, noConflict]])
-
Imports all library functions into a context.
Parameters:
- context
a context. Defaults to
_G
(global environment) when not given. (optional) - noConflict if supplied, will not import functions having a key existing in the destination context. (optional)
Returns:
-
the passed-in context
- context
a context. Defaults to