loop_flat_map!
Concatenate the values a block returns over a static number of iterations. Loops and mapping
loop_flat_map! count[, i] { ... }
Runs the block count times, each time in a fresh inner scope with the optional counter bound to the static iteration index, and concatenates the block's final expressions in order. This is the usual way to build a vector piece by piece, e.g. gathering slices of another vector.
Arguments
count- Static scalar, the number of iterations.
i- Optional. A bare variable name bound to the static iteration index.
{ ... }- Required block ending in a final expression, a non-empty vector. Outer variables can be read and assigned as in
loop!.
Returns
The concatenation of the block values, an expression. It is static if every block value is static and a witness if every block value is a witness.
Constraints
None of its own; the block's operations add theirs. Behaves the same in the static VM.
Example
(let x) loop_flat_map! 3, i { x[2 - i] * (i + 1) }
Notes
- All block values must have the same location: concatenating a static block value with a witness one fails with
variables are of different types. Aletbinding of a static value makes it a witness constant. - With
count0 it returns the empty vector without running the block. - Errors if the block has no final expression, returns a tuple or an empty vector.
- Returning whole slices per iteration (as
rotl32_bitsdoes) compiles much faster than one element per iteration.
Used in the standard library
bits/add32_bits, bits/bytes_to_bits, bits/bytes_to_bits_static, bits/rotl32_bits, blake3/blake3_compress, blake3/blake3_core, blake3/blake3_rotate_words, dilithium/lattice/lattice_permutation, ext/ext_mle_eval, ext/ext_mul, keccak/keccak_f1600, keccak/keccak_sponge_shake, lattice/lattice_bits_vec, lattice/lattice_expand, mldsa/mldsa_limbs, mldsa/mldsa_rej_ntt_poly, oraccle/oraccle_sample_uniform_vec, polynomial/mat_poly_compose_horizontal, polynomial/mat_poly_iden, polynomial/vec_polynomial_coef_mul, r1cs/r1cs_from_output, shift, size_cyclic, slip/slip_r1cs_arg_verify, typed/typed_bytes_check, typed/typed_bytes_lattice, typed/typed_lex_chacha, valloc