reduce.nv
· 1.6 KiB · Text
Surowy
Playground
| 'DOCUMENTATION' | ~~
the @reduce library allows you to reduce RPN expressions
that are enclosed within curly braces inside of a tuple .
here's an example of how to use it :
'' (@reduce snake = x is { 2 3 + 4 * } and y is { 10 7 - } .)
this will result in the following :
'snake' x is 20 and y is 3
also note that you can replace the stack name with @main to
push the result to the main stack .
| '' (@reduce .) | '' convert output stack to tuple
| '' @reduce? '' ($stack =) | 'destination' $stack
| '' @reduce? '' ({ }) 'numbers' $result | 'output' $result
| '' @reduce? '' {? '' + 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) + Number($b)));
| '' @reduce? '' {? '' - 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) - Number($b)));
| '' @reduce? '' {? '' * 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) * Number($b)));
| '' @reduce? '' {? '' / 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) / Number($b)));
| '' @reduce? '' {? '' random 'numbers' ($max $min) | '@js'
const min = Number($min), max = Number($max);
f("numbers", min + Math.floor(Math.random() * (max - min)));
| '' @reduce? '' {? '' wrap 'numbers' ($max $min $a) | '@js'
const a = Number($a), min = Number($min), max = Number($max);
let result = a;
if (a < min) result = max;
if (a > max) result = min;
f("numbers", result);
| '' @reduce? '' {? '' $number| 'numbers' $number
| '' @reduce? '' $word | 'output' $word
| '' convert $stack stack to tuple 'destination' $destination | '@js'
let destination = $destination == "@main" ? "" : $destination;
f(destination, ...stacks[$stack].map(fact => fact.join(" ")));
delete stacks[$stack];
| 1 | | 'DOCUMENTATION' | ~~ |
| 2 | the @reduce library allows you to reduce RPN expressions |
| 3 | that are enclosed within curly braces inside of a tuple . |
| 4 | here's an example of how to use it : |
| 5 | |
| 6 | '' (@reduce snake = x is { 2 3 + 4 * } and y is { 10 7 - } .) |
| 7 | |
| 8 | this will result in the following : |
| 9 | |
| 10 | 'snake' x is 20 and y is 3 |
| 11 | |
| 12 | also note that you can replace the stack name with @main to |
| 13 | push the result to the main stack . |
| 14 | |
| 15 | |
| 16 | | '' (@reduce .) | '' convert output stack to tuple |
| 17 | | '' @reduce? '' ($stack =) | 'destination' $stack |
| 18 | | '' @reduce? '' ({ }) 'numbers' $result | 'output' $result |
| 19 | |
| 20 | | '' @reduce? '' {? '' + 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) + Number($b))); |
| 21 | | '' @reduce? '' {? '' - 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) - Number($b))); |
| 22 | | '' @reduce? '' {? '' * 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) * Number($b))); |
| 23 | | '' @reduce? '' {? '' / 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) / Number($b))); |
| 24 | |
| 25 | | '' @reduce? '' {? '' random 'numbers' ($max $min) | '@js' |
| 26 | const min = Number($min), max = Number($max); |
| 27 | f("numbers", min + Math.floor(Math.random() * (max - min))); |
| 28 | |
| 29 | | '' @reduce? '' {? '' wrap 'numbers' ($max $min $a) | '@js' |
| 30 | const a = Number($a), min = Number($min), max = Number($max); |
| 31 | let result = a; |
| 32 | if (a < min) result = max; |
| 33 | if (a > max) result = min; |
| 34 | f("numbers", result); |
| 35 | |
| 36 | | '' @reduce? '' {? '' $number| 'numbers' $number |
| 37 | | '' @reduce? '' $word | 'output' $word |
| 38 | |
| 39 | |
| 40 | | '' convert $stack stack to tuple 'destination' $destination | '@js' |
| 41 | let destination = $destination == "@main" ? "" : $destination; |
| 42 | f(destination, ...stacks[$stack].map(fact => fact.join(" "))); |
| 43 | delete stacks[$stack]; |
| 44 |