@lang:map
Transform each element of an array by applying a helper.
On-chain (@lang:map!): The transform is a named def @name! of one parameter, applied by name; a composed body costs more per element.
Returns: array
Syntax
Section titled “Syntax”@lang:map(arr fn)Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
arr | array | Source array |
fn | helper | Transform helper applied to each element |
See Also
Section titled “See Also”- @filter — keep elements by predicate
- @reduce — fold an array to a single value
- loop — imperative iteration
On-chain face (@map!)
Section titled “On-chain face (@map!)”Transform every element of the array return of a call on-chain through
mapWords. The transform is a def @name! of one parameter,
applied by name with its parameter substituted at each occurrence (def @dbl! "$x: number -> number" @num!($x * 2) maps each
element to element * 2). A lambda reducing to one Operators call runs
as a single staticcall per element; a composed one (a nested live call,
a multi-call body like @num!($x * 2 + 1)) routes through the core
and costs several.
The result is the mapped words payload (a bytes value), composable with
the other array faces: @reduce!(@map!(…) add 0), @sort!(@map!(…)).
Examples
Section titled “Examples”load lang
set $vault 0x44fA8E6f47987339850636F88629646662444217
# Sum of the doubled capsdef @dbl! "$x: number -> number" @num!($x * 2)assert @reduce!(@map!($vault::{caps()(uint256[])} @dbl!) add 0) >= 100- Arrays of single-word elements only; the lambda output is one word per element.
- The signed sort recipe rides on @map!: flip the sign bit, sort, flip back.
- Naming the parameter more than once substitutes at each place it
appears, so
@num!($x * $x)squares: two windows, one call.
See Also
Section titled “See Also”assert,@reduce!,@all!