Modul:fun/doc
fun
stands for "functional", but also functional programming can be fun. This library contains some typical metafunctions for functional programming, such as map
, some
, all
, curry
, as well as others.
Functions that take an array as their second argument are available as methods in the arrays created by Module:array, with the arguments reversed so that they can be called as methods.
It was started in a user sandbox (Module:User:Erutuon/functional). It is not to be confused with Lua Fun (of which there is a version at Module:User:Erutuon/luafun), even though some functions are similar.
The functions that take a table as their second argument will treat the table as an array if t[1]
is not nil
, and use ipairs
. Otherwise, they will treat it as a hashmap, using pairs
.
function map(func, iterable)
- Perform a function
func
on every element initerable
and return the resulting table.iterable
may be a table or a string. If a table, the function operates on every element in the array portion of the table; if a string, the function operates on every UTF-8 character in the string. The functionfunc
has the following signature:func(member, i, iterable)
. That is, the table element or UTF-8 character is first, then the index of this element, and then the iterable value (table or string). function mapIter(func, iterator, iterable, initial_value)
- Create a new array from the results of performing a function on the values returned by an iterator. Can be used in combination with
sortedPairs
in Module:table.func
has the same signature described above. Not very useful withgmatch
;func
would have anil
first argument, because the single value returned from the iterator would be supplied as the second argument tofunc
. mapIter( function(parameter_value, parameter_name) return { parameter_name, parameter_value } end, sortedPairs(frame.args)) --> returns a sorted array of arrays containing parameter names and values