@lang:sum
Sum the elements of an array.
Returns: number
Syntax
Section titled “Syntax”@lang:sum(arr)Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
arr | array | Source array |
See Also
Section titled “See Also”On-chain face (@sum!)
Section titled “On-chain face (@sum!)”Sum the array return of a call into one word, on-chain: a native
sumWords over the word payload. This is the fixed-operation form of
@reduce!(add 0) (one on-chain loop instead of a per-element foldWords
lambda call, so it is cheaper). Reach for @reduce! when you need a
different reduction (min, max, bitOr, bitAnd) or a nonzero initial
accumulator.
Examples
Section titled “Examples”load lang
set $vault 0x44fA8E6f47987339850636F88629646662444217
# The caps sum to at least 100assert @sum!($vault::{caps()(uint256[])}) >= 100
# Sum a mapped payload (double each element first)def @dbl! "$x: number -> number" @num!($x * 2)assert @sum!(@map!($vault::{caps()(uint256[])} @dbl!)) >= 200- Arrays of single-word elements only; the result is judged as a uint word (the checked sum overflows-reverts past 2^256 - 1).
- An empty array sums to 0.
- An average is
@num!(@sum!(...) / @len!(...)).
See Also
Section titled “See Also”assert,@reduce!,@len!