mjolnir.fnutils

Super-helpful functional programming utilities.

concat

mjolnir.fnutils.concat(t1, t2)

Adds all elements of t2 to the end of t1.

contains

mjolnir.fnutils.contains(t, el) -> bool

Returns whether the table contains the given element.

copy

mjolnir.fnutils.copy(t) -> t2

Returns a new copy of t using pairs(t).

cycle

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}

each

mjolnir.fnutils.each(t, fn) -> t

Runs fn(el) for every el in t.

filter

mjolnir.fnutils.filter(t, fn) -> t

Returns a table of the elements in t in which fn(el) is truthy.

find

mjolnir.fnutils.find(t, fn) -> el

Returns the first element where fn(el) is truthy.

indexof

mjolnir.fnutils.indexof(t, el) -> int or nil

Returns the index of a given element in a table, or nil if not found.

map

mjolnir.fnutils.map(t, fn) -> t

Returns a table of the results of fn(el) on every el in t.

mapcat

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.

partial

mjolnir.fnutils.partial(fn, ...) -> fn'

Returns fn partially applied to arg (...).

reduce

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.

sequence

mjolnir.fnutils.sequence(...) -> fn

Returns a list of the results of the passed functions.