flat_map!
Map a block over the elements of a vector and concatenate the results. Loops and mapping
flat_map! vector, v[, i] { ... }
Runs the block once per element of vector, with v bound to the element (a scalar) and the optional counter to its static index, each in a fresh inner scope. The block values are concatenated in order.
Arguments
vector- Static or witness vector. It is evaluated (and moved, if it is a named vector) once.
v- A bare variable name bound to the current element.
i- Optional. A bare variable name bound to the static element index.
{ ... }- Required block ending in a final expression, a non-empty vector.
Returns
The concatenation of the block values, an expression; static if every block value is static, a witness if every block value is a witness.
Constraints
None of its own. Behaves the same in the static VM.
Example
(let x) flat_map! x, v, i { concat! v, v * (i + 1) }
Notes
- Mixing static and witness block values fails with
variables are of different types. - Errors on an empty input vector and on a block returning a tuple or an empty vector.
- See
loop_flat_map!to iterate over indices andchunks_flat_map!to iterate over chunks.
Used in the standard library
oraccle/oraccle_sample_ternary_vec, slip/slip_r1cs_arg_verify