reduce.nv
· 1.4 KiB · Text
Brut
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 .
|| '@include' lib/math.nv
| '' (@reduce .) | '' convert output stack to tuple
| '' (@reduce $stack =) | '' @reduce 'destination' $stack
| '' (@reduce {) '@math' $result | '' (@reduce { $result)
| '' (@reduce { $result }) | '' @reduce 'output' $result
| '' (@reduce { $a $b +) | '' (@reduce {) '@math' add $a $b
| '' (@reduce { $a $b -) | '' (@reduce {) '@math' subtract $a $b
| '' (@reduce { $a $b *) | '' (@reduce {) '@math' multiply $a $b
| '' (@reduce { $a $b /) | '' (@reduce {) '@math' divide $a $b
| '' (@reduce { $a $min $max wrap) | '' (@reduce { $result) '@js'
const a = Number($a), min = Number($min), max = Number($max);
$result = a;
if (a < min) $result = max;
if (a > max) $result = min;
| '' (@reduce $word) | '' @reduce '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 | || '@include' lib/math.nv |
| 17 | |
| 18 | | '' (@reduce .) | '' convert output stack to tuple |
| 19 | | '' (@reduce $stack =) | '' @reduce 'destination' $stack |
| 20 | | '' (@reduce {) '@math' $result | '' (@reduce { $result) |
| 21 | | '' (@reduce { $result }) | '' @reduce 'output' $result |
| 22 | | '' (@reduce { $a $b +) | '' (@reduce {) '@math' add $a $b |
| 23 | | '' (@reduce { $a $b -) | '' (@reduce {) '@math' subtract $a $b |
| 24 | | '' (@reduce { $a $b *) | '' (@reduce {) '@math' multiply $a $b |
| 25 | | '' (@reduce { $a $b /) | '' (@reduce {) '@math' divide $a $b |
| 26 | | '' (@reduce { $a $min $max wrap) | '' (@reduce { $result) '@js' |
| 27 | const a = Number($a), min = Number($min), max = Number($max); |
| 28 | $result = a; |
| 29 | if (a < min) $result = max; |
| 30 | if (a > max) $result = min; |
| 31 | | '' (@reduce $word) | '' @reduce 'output' $word |
| 32 | |
| 33 | | '' convert $stack stack to tuple 'destination' $destination | '@js' |
| 34 | let destination = $destination == "@main" ? "" : $destination; |
| 35 | f(destination, ...stacks[$stack].map(fact => fact.join(" "))); |
| 36 | delete stacks[$stack]; |
| 37 |