Skip to content

@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

@lang:map(arr fn)
NameTypeDescription
arrarraySource array
fnhelperTransform helper applied to each element
  • @filter — keep elements by predicate
  • @reduce — fold an array to a single value
  • loop — imperative iteration

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!(…)).

load lang
set $vault 0x44fA8E6f47987339850636F88629646662444217
# Sum of the doubled caps
def @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.
  • assert, @reduce!, @all!