Higher order functions
Operation | Description |
---|---|
apply |
Apply a function using arguments on stack |
eval |
Evaluate top of stack |
filter |
Filter items in the stack |
fold, reduce |
Reduce items to a single value |
map |
Apply an operation |
repeat |
Repeat the execution of an operation |
Evaluates the expression in fn by first popping nargs off the stack and pushing them back as a single argument. This is useful for higher order functions, like map, where some of the arguments are from existing results found on the stack.
Stack effects:
( args:Any* expr:Text n:Int/u32 -- Any* )
Example:
Input | Stack |
---|---|
1 2 3 4 |
1 | 2 | 3 | 4 |
n |
1 | 2 | 3 | 4 | 4 |
[swap sub] [map] 2 apply |
3 | 2 | 1 | 0 |
Evaluate expr as if it was input to the calculator.
Stack effects:
( expr:Text Any* -- Any* )
Example:
Input | Stack |
---|---|
'1 2 add' eval |
3 |
Filter the stack by keeping items that are true when evaluated by expression expr.
Stack effects:
( Any* expr:Text -- Any* )
Example:
Input | Stack |
---|---|
1 2 3 4 5 6 |
1 | 2 | 3 | 4 | 5 | 6 |
[2 rem 0 eq] filter |
2 | 4 | 6 |
Reduce the stack to a single value using the expression expr. An invalid argument error is raised if expr does not reduce.
Alias: reduce
Stack effects:
( Any* expr:Text -- Any* )
Example:
Input | Stack |
---|---|
1 2 3 4 5 |
1 | 2 | 3 | 4 | 5 |
/add fold |
15 |
Apply the expression, expr, to each item on the stack.
Stack effects:
( expr:Text Any* -- Any* )
Example:
Input | Stack |
---|---|
1 2 3 4 5 |
1 | 2 | 3 | 4 | 5 |
[2 mul] map |
2 | 4 | 6 | 8 | 10 |
Repeat execution of expression expr for n times.
Stack effects:
( Any* expr:Text n:Int/u -- )
Example:
Input | Stack |
---|---|
1 |
1 |
[2 mul] 8 repeat |
256 |