Super-helpful functional programming utilities.
mjolnir.fnutils.concat(t1, t2)
Adds all elements of t2 to the end of t1.
mjolnir.fnutils.contains(t, el) -> bool
Returns whether the table contains the given element.
mjolnir.fnutils.copy(t) -> t2
Returns a new copy of t using pairs(t).
mjolnir.fnutils.cycle(t) -> fn() -> t[n]
Returns a function that returns t[1], t[2], ... t[#t], t[1], ... on successive calls.
Example:
f = cycle({4, 5, 6})
{f(), f(), f(), f(), f(), f(), f()} == {4, 5, 6, 4, 5, 6, 4}
mjolnir.fnutils.each(t, fn) -> t
Runs fn(el) for every el in t.
mjolnir.fnutils.filter(t, fn) -> t
Returns a table of the elements in t in which fn(el) is truthy.
mjolnir.fnutils.find(t, fn) -> el
Returns the first element where fn(el) is truthy.
mjolnir.fnutils.indexof(t, el) -> int or nil
Returns the index of a given element in a table, or nil if not found.
mjolnir.fnutils.map(t, fn) -> t
Returns a table of the results of fn(el) on every el in t.
mjolnir.fnutils.mapcat(t, fn) -> t2
Runs fn(el) for every el in t, and assuming the results are tables, combines them into a new table.
mjolnir.fnutils.partial(fn, ...) -> fn'
Returns fn partially applied to arg (...).
mjolnir.fnutils.reduce(t, fn) -> t2
Runs fn(el1, el2) for every el in t, then fn(result, el3), etc, until there's only one left.
mjolnir.fnutils.sequence(...) -> fn
Returns a list of the results of the passed functions.