@lang:count
Count the elements of an array that satisfy the predicate.
Returns: number
Syntax
Section titled “Syntax”@lang:count(arr fn)Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
arr | array | Source array |
fn | helper | Predicate helper returning bool |
Examples
Section titled “Examples”load lang [@count]
def @isBig "$n: number -> bool" @bool($n > 2)
# 2print @count([1 2 3 4] @isBig)See Also
Section titled “See Also”- @filter: keep the matching elements
- @any: true if at least one matches
- @all: true if all match
- @len: the number of elements
On-chain face (@count!)
Section titled “On-chain face (@count!)”Count the elements for which a named boolean callback returns true. Empty arrays count 0. Callbacks support composed helpers and ABI calls over typed single-word or multiword elements.
Examples
Section titled “Examples”load lang
set $vault 0x44fA8E6f47987339850636F88629646662444217set $token 0x6B175474E89094C44Da98b954EedeAC495271d0F
# At least two caps are 100 or moredef @ge100! "$x: number -> bool" @bool!($x >= 100)assert @count!($vault::!{caps()(uint256[])} @ge100!) >= 2
# At least three holders have a balance of 1000 or moredef @funded! "$who: address -> bool" @bool!($token::!{balanceOf(address)(uint256) $who} >= 1000)assert @count!($vault::!{holders()(address[])} @funded!) >= 3- Same result as
@len!(@filter!(...)), without building the filtered array. - A predicate that compares one call over the element with a value, such as a balance against a minimum, costs one call per element. The value may be a call of its own: it is read once, not once per element.