Foreward is a Lua library of functional programming helpers, inspired by LiveScript, Haskell and Clojure. Most descriptions come from Prelude.ls or Underscore.js.
Returns length
of the list or string.
len({1, 2, 3})
=> 3
len("hey")
=> 3
Concatenates lists.
cat({1, 2}, {3, 4})
=> {1, 2, 3, 4}
Returns new list with items added to the end.
conj(1, 2, {3, 4})
=> {3, 4, 1, 2}
Applies a function to each item in the list and returns the original list. Used for side effects.
each(function(x) print(x) end, {1, 2, 3})
=> 1 2 3
Applies a function to each item in the list, and produces a new list with the results. The length of the result is the same length as the input.
map(function(x) return x + 1 end, {1, 2, 3})
=> {2, 3, 4}
Returns a new list which contains only the truthy values of the inputted list.
compact({0, 1, false, nil, "", "ha"})
=> {0, 1, "", "ha"}
Returns a new list composed of the items which pass the supplied function's test.
filter(function(x) return even(x) end, {1, 2, 3})
=> {2}
Like filter
, but the new list is composed of all the items which fail the function's test.
reject(function(x) return even(x) end, {1, 2, 3})
=> {1, 3}
Takes a list of items, and using the binary function supplied, folds them into a single value. Requires an initial value (the second argument), which will be the starting point, and result in case of an empty list.
fold(function(x, y) return x + y end, 0, {1, 2, 3})
=> 6
Sums up the values in the list.
sum({1, 2, 3})
=> 6
Gets the product of all the items in the list.
prod({1, 2, 3})
=> 6
Gets the mean of the values in the list.
mean({1, 2, 3})
=> 2
Flattens a list which contains sub lists and elements of arbitrary depth into a list of elements with no sub lists.
flat({1, {{2}, 3}, {4, {{5}}}})
=> {1, 2, 3, 4, 5}
Returns a section of a list.
slice(2, 4, {1, 2, 3, 4, 5})
=> {3, 4}
Zips together its two arguments into a list of lists.
zip({1, 2, 3}, {4, 5, 6})
=> {{1, 4}, {2, 5}, {3, 6}}
A function to create flexibly-numbered lists of integers, handy for each and map loops. start, if omitted, defaults to 1; step defaults to 1. Returns a list of integers from start to stop, incremented (or decremented) by step, exclusive. Note that ranges that stop before they start are considered to be zero-length instead of negative — if you'd like a negative range, use a negative step.
range(5)
=> {1, 2, 3, 4, 5}
The first item of the list. Returns nil
if the list is empty.
head({1, 2, 3, 4, 5})
=> 1
The last item of the list. Returns nil
if the list is empty.
last({1, 2, 3, 4, 5})
=> 5
Everything but the last item of the list.
init({1, 2, 3, 4, 5})
=> {1, 2, 3, 4}
Everything but the first item of the list.
tail({1, 2, 3, 4, 5})
=> {2, 3, 4, 5}
Returns a new list which is the reverse of the inputted one.
rev({1, 2, 3, 4, 5})
=> {5, 4, 3, 2, 1}
Sorts a list. Does not modify the input.
sort({3, 1, 2})
=> {1, 2, 3}
Returns a new list which contains each value of the inputted list only once.
uniq({1, 1, 3, 2})
=> {1, 3, 2}
Equivalent to {filter(f, xs), reject(f, xs)}
, but more efficient, using only one loop.
part(function(x) return even(x) end, {1, 2, 3, 4, 5})
=> {{2, 4}, {1, 3, 5}}
Returns a copy of the array with all instances of the values removed.
without(0, 1, {1, 2, 1, 0, 3, 1, 4})
=> {2, 3, 4}
Produces a new list containing all the elements of all the inputted lists only once.
union({1, 5, 7}, {3, 5})
=> {1, 5, 7, 3}
Produces a new list containing all the items which are present in all the lists.
same({1, 2, 3}, {101, 2, 1, 10})
=> {1, 2}
Returns a new list containing the elements which are present in the first list and not in the others.
diff({1, 2, 3}, {1})
=> {2, 3}
Returns the first n
items in the list.
take(2, {1, 2, 3, 4, 5})
=> {1, 2}
Returns the result of dropping the first n
items of the list.
drop(2, {1, 2, 3, 4, 5})
=> {3, 4, 5}
Gets the element at the nth
index (the first argument). If negative, will work from the end.
nth(2, {1, 2, 3, 4, 5})
=> 2
Returns the index at which value can be found in the array, or nil
if value is not present in the array.
index(2, {1, 2, 3, 4, 5})
=> 2
Returns the first item in list to pass the function's test. Returns nil
if all items fail the test.
find(odd, {2, 3, 4 ,5})
=> 3
Returns a shuffled list.
shuffle({1, 2, 3, 4})
=> {2, 4, 1, 3}
Produce a random sample from the list. Pass a number to return n
random elements from the list. Otherwise a single random item will be returned.
sample({1, 2, 3, 4, 5})
=> 4
sample(3, {1, 2, 3, 4, 5})
=> {1, 4, 2}
Takes a list of comparable items, and returns the smallest of them.
least({"c", "e", "a", "d", "b"})
=> "a"
Takes a list of comparable items, and returns the largest of them.
most({4, 1, 9, 3})
=> 9
Returns true
if the value is present in the list.
has(3, {1, 2, 3, 4, 5})
=> true
Returns true
if all the items in the list are true when applied to the test.
all(odd, {1, 2, 3, 4, 5})
=> false
Returns true
if any of the items in the list are true when applied to the test.
any(odd, {1, 2, 3, 4, 5})
=> true
Whether the list is empty.
empty({})
=> true
Returns value
minus 1.
dec(10)
=> 9
Returns value
plus 1.
inc(10)
=> 11
The negation of the inputted number.
neg(1)
=> -1
Division truncated toward 0.
quot(-20, 3)
=> -6
Remainder, like the %
operator.
rem(-20, 3)
=> -2
Division truncated down toward negative infinity.
div(-20, 3)
=> -7
Provides true mathematical modulo.
mod(-20, 3)
=> 1
Is the number even?
even(4)
=> true
Is the number odd?
odd(3)
=> true
Greatest common denominator.
gcd(12, 18)
=> 6
Least common multiple.
lcm(12, 18)
=> 36
Splits a string on a separator into a list of strings.
split("|", "1|2|3")
=> {"1", "2", "3"}
Joins a list with the specified separator.
join("|", {1, 2, 3})
=> "1|2|3"
Splits a string at newlines into a list.
lines("one\ntwo\nthree")
=> {"one", "two", "three"}
Joins a list of strings into a single string using newlines.
unlines({"one", "two", "three"})
=> "one\ntwo\nthree"
Splits a string at spaces (one or more), returning a list of strings.
words("hello, how are you?")
=> {"hello,", "how", "are", "you?"}
Joins a list of strings into a single string using spaces.
unwords({"one", "two", "three"})
=> "one two three"
Splits a string at every character, returning a list of one character strings.
chars("hello")
=> {"h", "e", "l", "l", "o"}
Joins a list of strings into a single string using no separator.
unchars({"h", "e", "l", "l", "o"})
=> "hello"
Capitalizes a string.
capitalize("hello, how are you?")
=> "Hello, how are you?"
Returns the application of the supplied list as arguments to the supplied function.
apply(math.min, {2, 5, 1})
=> 1