types.Table.each
Calls callback once for every slot in the table.
The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing
... from the prose below, not from the signature.Parameters
callback | function | callback(value, [key], [table]), called for every slot |
Return value
Nothing (null); each is used for its side effects.
Errors
Whatever callback throws propagates out of each.
Notes
Takes exactly one argument, callback.
callback gets exactly as many of the arguments listed above as it declares parameters for, and never more; one that declares none is called once per slot with no arguments. Use this to count slots or to repeat a side effect.
Iteration order is whatever the table's current order happens to be, which the VM does not guarantee to be stable across runs or seeds - do not let callback's side effects depend on the order slots are visited in.
Example
let t = {a=1, b=2, c=3}
local total = 0
t.each(function(v) { total += v }) // 1-param: value only
println("total =", total)
let seen = {}
t.each(function(v, k) { seen[k] <- v }) // 2-param: value and key
println("seen.len() == t.len():", seen.len() == t.len())
let r = t.each(function(v) {})
println("r == null:", r == null) // each returns nothingOutput:
total = 6
seen.len() == t.len(): true
r == null: trueSee also
map | Returns a new table with the same keys, each mapped through callback. |
filter | Returns a new table holding only the slots for which callback returns a truthy result. |
reduce | Combines every value in the table into one result by calling callback repeatedly. |
| table | class index |