Skip to content

@lang:filter

Keep elements of an array for which a helper returns truthy.

On-chain (@lang:filter!): The predicate is a named def @name! of one parameter returning bool, applied by name.

Returns: array

@lang:filter(arr fn)
NameTypeDescription
arrarraySource array
fnhelperPredicate helper returning bool
  • @find — return the first match
  • @all — check if all elements match
  • @any — check if any element matches
  • @map — transform each element

Keep the matching elements of the array return of a call on-chain through filterWords. The predicate is a def @name! of one parameter returning bool (a def whose body is @bool!($x >= 100) keeps each element with element >= 100) — the same lambda machinery @map! and @all! use. A predicate reducing to one Operators call runs as a single staticcall per element; a composed one (a nested live call, a multi-call expression) routes through the core and costs several.

The result is the kept words payload (a bytes value) in source order, composable with the other array faces: @len!(@filter!(…)), @reduce!(@filter!(…) add 0), @sort!(@filter!(…)).

load lang
set $vault 0x44fA8E6f47987339850636F88629646662444217
# Exactly two caps at or above the floor
def @ge100! "$x: number -> bool" @bool!($x >= 100)
assert @len!(@filter!($vault::{caps()(uint256[])} @ge100!)) == 2
  • Arrays of single-word elements only; the output length is the kept count, so the payload nests into @len!, @at!, the folds and the other word ops.
  • One staticcall per element: gas bounds the practical array size.
  • Naming the parameter more than once substitutes at each place it appears, so @num!($x * $x) squares: two windows, one call.
  • assert, @map!, @find!, @all!