Module:Fun/doc

Revision as of 04:34, 2 January 2021 by Chrysophylax (talk | contribs) (Created page with "<code>fun</code> stands for "functional", but also functional programming can be fun. This library contains some typical metafunctions for functional programming, such as {{co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is the documentation page for Module:Fun

fun stands for "functional", but also functional programming can be fun. This library contains some typical metafunctions for functional programming, such as <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">map</syntaxhighlight>, <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">some</syntaxhighlight>, <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">all</syntaxhighlight>, <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">curry</syntaxhighlight>, 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 <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">t[1]</syntaxhighlight> is not <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">nil</syntaxhighlight>, and use <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">ipairs</syntaxhighlight>. Otherwise, they will treat it as a hashmap, using <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">pairs</syntaxhighlight>.

<syntaxhighlight inline="1" lang="lua" style="white-space
pre-wrap;">function map(func, iterable)</syntaxhighlight>
Perform a function <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func</syntaxhighlight> on every element in <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">iterable</syntaxhighlight> and return the resulting table. <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">iterable</syntaxhighlight> 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 function <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func</syntaxhighlight> has the following signature: <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func(member, i, iterable)</syntaxhighlight>. 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).
<syntaxhighlight inline="1" lang="lua" style="white-space
pre-wrap;">function mapIter(func, iterator, iterable, initial_value)</syntaxhighlight>
Create a new array from the results of performing a function on the values returned by an iterator. Can be used in combination with <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">sortedPairs</syntaxhighlight> in Module:table. <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func</syntaxhighlight> has the same signature described above. Not very useful with <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">gmatch</syntaxhighlight>; <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func</syntaxhighlight> would have a <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">nil</syntaxhighlight> first argument, because the single value returned from the iterator would be supplied as the second argument to <syntaxhighlight inline="1" lang="lua" style="white-space:pre-wrap;">func</syntaxhighlight>.
<source lang="lua">

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 </source>