LibFunctional-1.0

all(list, fn)

Returns true if all the values in the list list satisfy the truth test fn, false otherwise.
aliases: every

Parameters

list
the input list.
fn
the truth test function.


any(list, fn)

Returns true if any value in the list list satisfies the truth test fn, false otherwise.
aliases: some

Parameters

list
the input list.
fn
the truth test function.


binary_search(list, value[, fn])

Performs a binary search on sorted list list for value value and returns its index and value if found

Parameters

list
the input sorted list.
value
the value to search for.
fn
an optional function that is applied to each value in the list before performing the comparison.


bind(fn, arg1[, arg2...])

Returns a function g such as calling g(p1, p2, ..
pn)
is equivalent to calling fn(arg1, arg2, .. argn, p1, p2, .. pn).

Parameters

fn
the input function.


bind_nth(fn, nth, arg1[, arg2...])

Returns a function g such as calling g(p1, p2, ..
pn)
is equivalent to calling fn(p1, p2, .. pnth, arg1, arg2, .. argn, pnth+1, pnth+2, .. pnth+n).

Parameters

fn
the input function.
nth
the position of the first argument to be bound.


clone(tbl[, deep])

Returns a copy of the table tbl.

Parameters

tbl
the input table.
deep
optional, if true, table values inside the table are copied recursively, otherwise a shallow copy is made. Note that if the tables contains cyclic references this function will fail to perform a deep copy.


concat(...)

Returns a list containing the concatenation of all the input lists.

Parameters

...
any number of input lists.


contains(list, value, fn)

Returns true if the value value is present in the list list, false otherwise.
aliases: elem

Parameters

list
the input list.
value
the value to search for.
fn
an optional function to apply to each value in the list before comparing it to value.


difference([fn, ]list1, [list2...])

Returns a list containing all the items in the first list that are not present in any of the rest.

Parameters

fn
an optional function to apply to each value in the list before performing the comparison.
list
two or more input lists.


each(list, fn)

Calls repeatedly the function fn with each value of the list list.
aliases: for_each

Parameters

list
the input list.
fn
the function called with each value.


equal(tbl1, tbl2[, deep])

Returns true if the table tbl1 has the same keys and values than the table tbl2.

Parameters

tbl1
first table.
tbl2
second table.
deep
optional, if true, table values inside the tables are compared recursively, otherwise a shallow comparison is made. Note that if the tables contains cyclic references this function will fail to perform a deep comparison.


filter(list, fn)

Returns a list of values in the list list that pass a truth test fn.

Parameters

list
the input list.
fn
the truth test function.


find_first_of(list, ...)

Returns the first value and its index in list list that is equal to any of the values passed.

Parameters

list
the input list.
...
one or more values to search for.


find_if(list, fn)

Returns the first value and its index in list list that passes the truth test fn.

Parameters

list
the input list.
fn
the truth test function.


find_last_of(list, ...)

Returns the last value and its index in list list that is equal to any of the values passed.

Parameters

list
the input list.
...
one or more values to search for.


flatten(list[, shallow])

Returns a copy of the list list with any nested lists flattened to a single level.

Parameters

list
the input list.
shallow
optional, if set to true only flattens the first level.


from_iterator([fn, ]f, s, var)

Returns a list constructed from the result of an iterator function.

Parameters

fn
an optional function that is applied to the values returned by the iterator before adding them to the list. If omitted, the default function packs all the values returned by the iterator into a list.
f
the values returned by an iterator function.
s
the values returned by an iterator function.
var
the values returned by an iterator function.


intersection([fn, ]list1, [list2...])

Returns a list containing all the items that are present in all of the input lists.
If the first list passed contains the same value multiple times, it may appear multiple times in the output list. Use uniq on the first list if you want to prevent this.

Parameters

fn
an optional function to apply to each value in the list before performing the comparison.
list
two or more input lists.


invert(tbl)

Returns a copy of the table tbl with its values as keys and its keys as values.

Parameters

tbl
the input table.


keys(tbl)

Returns a list of keys in the table tbl.

Parameters

tbl
the input table.


map(list, fn)

Returns a new list with the results of fn applied to all items in the list list.

Parameters

list
the input list.
fn
the function called with each value.


max(list)

Returns the maximum value in the list list.
This function is equivalent to reduce(list, math.max).

Parameters

list
the input list.


merge(...)

Returns a new table with the keys and values of all the passed tables.
If a key is present in more than one of the tables, the value from the rightmost table in the argument list is used.

Parameters

...
any number of input tables.


min(list)

Returns the minimum value in the list list.
This function is equivalent to reduce(list, math.min).

Parameters

list
the input list.


pairs(tbl)

Returns a list of { key, value } pairs in the table tbl.
This function is equivalent to zip(keys(tbl), values(tbl)).

Parameters

tbl
the input table.


range([start], stop[, step])

Returns a list containing the numbers from start to stop (including stop) with step step.
If omitted, start and step default to 1.


reduce(list, fn[, initial])

Returns a reduction of the list list based on the left associative application of the function fn.
aliases: foldl

Parameters

list
the input list.
fn
a function receiving two values representing the result of the previous application of this function and the next value in the list list.
initial
an optional initial value to be passed together with the first value of the list list to the function fn. If omitted, the first call is passed the two first values in the list list instead.


reduce_right(list, fn[, initial])

Returns a reduction of the list list based on the right associative application of the function fn.
aliases: foldr

Parameters

list
the input list.
fn
a function receiving two values representing the result of the previous application of this function and the previous value in the list list.
initial
an optional initial value to be passed together with the last value of the list list to the function fn. If omitted, the first call is passed the two last values in the list list instead.


reverse(list)

Returns a reversed copy of the list list.

Parameters

list
the input list.


shuffle(list)

Returns a copy of the list list shuffled using the Fisher–Yates algorithm.

Parameters

list
the input list.


shuffle_inplace(list)

Shuffles the list list in-place using the Fisher–Yates algorithm and returns it.

Parameters

list
the input list.


size(tbl)

Returns the number of items in the table tbl.

Parameters

tbl
the input table.


slice(list, begin[, end])

Returns a copy of a portion of the list list.

Parameters

list
the input list.
begin
the first index to copy. If negative, indicates an offset from the end of the list.
end
optional, the last index to copy. If omitted, the list is copied through the end. If negative, indicates an offset from the end of the list.


sort(list[, comp])

Returns a sorted copy of the list list.

Parameters

list
the input list.
comp
an optional comparison function that receives two values and returns true when the first is less than the second.


sort_inplace(list[, comp])

Performs an in-place sort of the list list and returns it.

Parameters

list
the input list.
comp
an optional comparison function that receives two values and returns true when the first is less than the second.


sorted_index(list, value[, fn])

Performs a binary search on sorted list list for value value and returns the index at which value should be inserted.

Parameters

list
the input sorted list.
value
the value to search for.
fn
an optional function that is applied to each value in the list before performing the comparison.


sorted_insert(list, value[, fn])

Inserts a value value in a sorted list list and returns it.

Parameters

list
the input sorted list.
value
the value to insert.
fn
an optional function that is applied to each value in the list before performing the comparison.


sum(list)

Returns a sum of all the values in the list list.
This function is equivalent to reduce(list, function(a, b) return a + b end).

Parameters

list
the input list.


union(...)

Returns a list containing all the different values present in the input lists.

Parameters

...
any number of input lists.


uniq(list[, is_sorted[, fn]])

Returns a copy of the list list with any duplicate values removed.

Parameters

list
the input list.
is_sorted
an optional argument specifying if the list is sorted, allowing to use a more efficient algorithm.
fn
an optional function that is applied to each value in the list before performing the comparison.


unzip(list)

Undoes a zip operation.

Parameters

list
a list of lists.

See also

  • zip


values(tbl)

Returns a list of values in the table tbl.

Parameters

tbl
the input table.


zip(...)

Takes any number of lists and returns a new list where each element is a list of the values in all of the passed lists at that position.
If one list is shorter than the others, excess elements of the longer lists are discarded.

Parameters

...
any number of input lists.

See also

  • unzip


zip_with(fn, ...)

Takes any number of lists and returns a new list where each element is the result of calling the function fn with the values in all of the passed lists at that position.
If one list is shorter than the others, excess elements of the longer lists are discarded.

Parameters

fn
a function taking as many parameters as lists are passed to zip_with and returning a value to be inserted in the resulting list.
...
any number of input lists.


You must login to post a comment. Don't have an account? Register to get one!

Table of contents

  1. 1 all(list, fn)
    1. 1.1 Parameters
  2. 2 any(list, fn)
    1. 2.1 Parameters
  3. 3 binary_search(list, value[, fn])
    1. 3.1 Parameters
  4. 4 bind(fn, arg1[, arg2...])
    1. 4.1 Parameters
  5. 5 bind_nth(fn, nth, arg1[, arg2...])
    1. 5.1 Parameters
  6. 6 clone(tbl[, deep])
    1. 6.1 Parameters
  7. 7 concat(...)
    1. 7.1 Parameters
  8. 8 contains(list, value, fn)
    1. 8.1 Parameters
  9. 9 difference([fn, ]list1, [list2...])
    1. 9.1 Parameters
  10. 10 each(list, fn)
    1. 10.1 Parameters
  11. 11 equal(tbl1, tbl2[, deep])
    1. 11.1 Parameters
  12. 12 filter(list, fn)
    1. 12.1 Parameters
  13. 13 find_first_of(list, ...)
    1. 13.1 Parameters
  14. 14 find_if(list, fn)
    1. 14.1 Parameters
  15. 15 find_last_of(list, ...)
    1. 15.1 Parameters
  16. 16 flatten(list[, shallow])
    1. 16.1 Parameters
  17. 17 from_iterator([fn, ]f, s, var)
    1. 17.1 Parameters
  18. 18 intersection([fn, ]list1, [list2...])
    1. 18.1 Parameters
  19. 19 invert(tbl)
    1. 19.1 Parameters
  20. 20 keys(tbl)
    1. 20.1 Parameters
  21. 21 map(list, fn)
    1. 21.1 Parameters
  22. 22 max(list)
    1. 22.1 Parameters
  23. 23 merge(...)
    1. 23.1 Parameters
  24. 24 min(list)
    1. 24.1 Parameters
  25. 25 pairs(tbl)
    1. 25.1 Parameters
  26. 26 range([start], stop[, step])
  27. 27 reduce(list, fn[, initial])
    1. 27.1 Parameters
  28. 28 reduce_right(list, fn[, initial])
    1. 28.1 Parameters
  29. 29 reverse(list)
    1. 29.1 Parameters
  30. 30 shuffle(list)
    1. 30.1 Parameters
  31. 31 shuffle_inplace(list)
    1. 31.1 Parameters
  32. 32 size(tbl)
    1. 32.1 Parameters
  33. 33 slice(list, begin[, end])
    1. 33.1 Parameters
  34. 34 sort(list[, comp])
    1. 34.1 Parameters
  35. 35 sort_inplace(list[, comp])
    1. 35.1 Parameters
  36. 36 sorted_index(list, value[, fn])
    1. 36.1 Parameters
  37. 37 sorted_insert(list, value[, fn])
    1. 37.1 Parameters
  38. 38 sum(list)
    1. 38.1 Parameters
  39. 39 union(...)
    1. 39.1 Parameters
  40. 40 uniq(list[, is_sorted[, fn]])
    1. 40.1 Parameters
  41. 41 unzip(list)
    1. 41.1 Parameters
    2. 41.2 See also
  42. 42 values(tbl)
    1. 42.1 Parameters
  43. 43 zip(...)
    1. 43.1 Parameters
    2. 43.2 See also
  44. 44 zip_with(fn, ...)
    1. 44.1 Parameters

Facts

Date created
Apr 21, 2013
Last updated
May 23, 2013

Author