Skip to content

each

Input Stack:
function: List
items: List
 
Output Stack:
function(items[N-1])
...
function(items[0])

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
PosInputOutput
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

  • :map - Apply function to each element and collect results into a new list
  • :call - Execute a single function/program
  • :list - Create lists that can be processed with each