Lambda functions are anonymous functions, means they have no name, and are defined inline. You can use lambda functions with array, set, maps and more MinitScript API, if a argument takes a Function.
As a statement lambda functions are written like
($arg0, ..., $argN) -> { console.printLine($arg0); ... }
If there are no lambda function arguments, you can write the lambda function also like this:
() -> { console.printLine("Test"); ... }
or
-> { console.printLine("Test"); ... }
See some examples:
Log array values to console using array.forEach.
$array = [1, 2, 3, 4, 5, 6]
$array->forEach(($element) -> { console.printLine($element) })
Log array values to console using array.forRange.
$array = [1, 2, 3, 4, 5, 6]
$array->forRange(($element) -> { console.printLine($element) }, 1, 3, 2)
Sum array values using array.forEach.
$array = [1, 2, 3, 4, 5, 6]
$sum = 0
$array->forEach(($element, &$sum) -> { $sum = $sum + $element }, $sum)
Sort array values descending.
$array = [1, 2, 3, 4, 5, 6]
$array->sort(($a, $b) -> { return($a > $b) })
Log set keys to console using set.forEach.
$set = {a, b, c}
$set->forEach(($key) -> { console.printLine($key) })
Log map key, value pairs to console using set.forEach.
$map = {a: 1, b: 2, c: 3}
$map->forEach(($key, $value) -> { console.printLine($key + " = " + $value) })
Sum map values using map.forEach.
$map = {a: 1, b: 2, c: 3}
$sum = 0
$map->forEach(($key, $value, &$sum) -> { $sum = $sum + $value }, $sum)
Using lambda functions in arrays.
$functions = [
($value) -> { console.printLine("function 1: " + $value) },
($value) -> { console.printLine("function 2: " + $value) },
($value) -> { console.printLine("function 3: " + $value) }
]
$value = 1
forEach($function in $functions)
script.callFunction($function, $value)
$value++
end
Using a lambda function with forCondition.
$i = 0
forCondition($i < 5, -> { $i++ })
console.printLine("$i = " + $i)
end
- Syntax
- Data types, variables and constants
- Flow control - if, elseif and else
- Flow control - switch, case and default
- Flow control - forTime, forCondition, for and forEach
- Flow control - exceptions
- Functions
- Lambda functions
- Classes
- Modules
- Built-in functions
- Built-in classes
- Operators
- Constants
- MinitScript, see README.md
- MinitScript - How to build, see README-BuildingHowTo.md
- MinitScript - How to use, see README-Tools.md
- The Mindty Kollektiv Discord Server