Guidelines:Modules: Difference between revisions
From Zelda Wiki, the Zelda encyclopedia
989 bytes removed
, 27 April 2020
|
|
Line 156: |
Line 156: |
|
| |
|
| A list of utility modules is available at [[:Category:Utility Modules]]. Leverage utility modules as much as possible so that the wiki's codebase stays {{Wp|Don't repeat yourself|DRY}}. | | A list of utility modules is available at [[:Category:Utility Modules]]. Leverage utility modules as much as possible so that the wiki's codebase stays {{Wp|Don't repeat yourself|DRY}}. |
|
| |
| ===Higher-Order Functions===
| |
| In utility modules such as [[Module:UtilsTable]], you'll often see functions like these:
| |
| <syntaxhighlight lang="lua">
| |
| utilsTable.isEqual({})({})
| |
| > true
| |
| </syntaxhighlight>
| |
|
| |
| You might've expected the usual function syntax <code>utilsTable.isEqual({}, {})</code> instead. The above is an example of a '''higher-order function'''—a function that returns a function. The function call above is shorthand for:
| |
| <syntaxhighlight lang="lua">
| |
| local isEmpty = utilsTable.isEqual({})
| |
| isEmpty({})
| |
| > true
| |
| </syntaxhighlight>
| |
|
| |
| Functions are written this way for increased reusability. They are often composed with the other type of higher-order function—one that accepts a function as an argument:
| |
| <syntaxhighlight lang="lua">
| |
| local isEmpty = utilsTable.isEqual({})
| |
| local notEmpty = utilsFunction.negate(isEmpty)
| |
| local magicWords = utilsTable.filter(notEmpty)({ {}, {}, {"Kooloo"}, {"Limpah"} })
| |
| utilsTable.flatten(magicWords)
| |
| > { "Kooloo", "Limpah" }
| |
| </syntaxhighlight>
| |
|
| |
|
| ==Exercises== | | ==Exercises== |