Anonymous

Module:FunList/Sandbox/doc: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
Line 1: Line 1:
This module allows for various complex querying and evaluating of a table or otherwise collection or object that is iterable. This means that every Lua type works that provides an ipairs or pairs iterator.
The functions under [[#Chainable Functions|Chainable Functions]] can be chained back to back to create a complex query. The underlying collection isn't evaluated unless some function is used that evaluates the function chain. This can be one of the many functions  not listed under Chainable Functions, or simply iterating over the function chain with ipairs/pairs.
To start create a new Enumerator3 object via Enumerator3.create. Functions can then be appended to this with the semicolon (:). Example:
<syntaxhighlight lang="lua">
local tbl = {}
local count = Enumerator3.create(tbl)
    :select(function(x) return x.number end)
    :countBy(function(x) return x % 2 == 0 end)
</syntaxhighlight>
The above snippet counts all numbers in the table under the 'number' field that are divisible by two. The countBy function evaluates all functions in the chain and returns the count.
All functions that have a "self" parameter can be used with any collection and doesn't need to be chained. For example, the maxBy function can be used to get the person with the highest age:
<syntaxhighlight lang="lua">
local tbl = {}
local oldestPerson = Enumerator3.maxBy(tbl, function(person) return person.age end)
</syntaxhighlight>
== Enumerator3 Initialisation ==
== Enumerator3 Initialisation ==
=== create ===
=== create ===
2,875

edits