each
Input Stack:
|
⇨ | Output Stack:
|
Execute a function for each element in a list, processing elements in order from first to last. Unlike :map, this operation executes the function for its side effects and places the results directly on the stack rather than collecting them into a new list.
The function is applied to each element individually, and the results are pushed onto the stack with the results from the last element ending up on top.
Parameters¶
- items: The input list containing elements to process
- function: A list representing the function/program to execute for each element
Examples¶
Apply the :dup
operation to each element in a list:
(,a,b,),(,:dup, ),:each
Pos | Input | Output |
---|---|---|
0 | List(:dup) | b |
1 | List(a, b) | b |
2 | a | |
3 | a |
This takes the list (a, b)
and applies :dup
to each element:
1. First processes a
, duplicating it to get a, a
(pushed to stack)
2. Then processes b
, duplicating it to get b, b
(pushed on top)
3. Final stack (top to bottom): b, b, a, a