autumn ревизій цього gist . До ревизії
1 file changed, 5 insertions
reduce.nv
@@ -21,6 +21,11 @@ | |||
21 | 21 | | '' @reduce? '' {? '' - 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) - Number($b))); | |
22 | 22 | | '' @reduce? '' {? '' * 'numbers' ($b $a) | '@js' f("numbers", "" + (Number($a) * Number($b))); | |
23 | 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 | + | ||
24 | 29 | | '' @reduce? '' {? '' wrap 'numbers' ($max $min $a) | '@js' | |
25 | 30 | const a = Number($a), min = Number($min), max = Number($max); | |
26 | 31 | let result = a; |
autumn ревизій цього gist . До ревизії
1 file changed, 16 insertions, 14 deletions
reduce.nv
@@ -13,22 +13,24 @@ | |||
13 | 13 | push the result to the main stack . | |
14 | 14 | ||
15 | 15 | ||
16 | - | || '@include' lib/math.nv | |
17 | - | ||
18 | 16 | | '' (@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' | |
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 | + | | '' @reduce? '' {? '' wrap 'numbers' ($max $min $a) | '@js' | |
27 | 25 | 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 | |
26 | + | let result = a; | |
27 | + | if (a < min) result = max; | |
28 | + | if (a > max) result = min; | |
29 | + | f("numbers", result); | |
30 | + | ||
31 | + | | '' @reduce? '' {? '' $number| 'numbers' $number | |
32 | + | | '' @reduce? '' $word | 'output' $word | |
33 | + | ||
32 | 34 | ||
33 | 35 | | '' convert $stack stack to tuple 'destination' $destination | '@js' | |
34 | 36 | let destination = $destination == "@main" ? "" : $destination; |
autumn ревизій цього gist . До ревизії
1 file changed, 5 insertions
reduce.nv
@@ -23,6 +23,11 @@ | |||
23 | 23 | | '' (@reduce { $a $b -) | '' (@reduce {) '@math' subtract $a $b | |
24 | 24 | | '' (@reduce { $a $b *) | '' (@reduce {) '@math' multiply $a $b | |
25 | 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; | |
26 | 31 | | '' (@reduce $word) | '' @reduce 'output' $word | |
27 | 32 | ||
28 | 33 | | '' convert $stack stack to tuple 'destination' $destination | '@js' |
autumn ревизій цього gist . До ревизії
1 file changed, 1 insertion, 2 deletions
reduce.nv
@@ -27,6 +27,5 @@ | |||
27 | 27 | ||
28 | 28 | | '' convert $stack stack to tuple 'destination' $destination | '@js' | |
29 | 29 | let destination = $destination == "@main" ? "" : $destination; | |
30 | - | const tuple = stacks[$stack].join(" "); | |
30 | + | f(destination, ...stacks[$stack].map(fact => fact.join(" "))); | |
31 | 31 | delete stacks[$stack]; | |
32 | - | f(destination, tuple); |
autumn ревизій цього gist . До ревизії
1 file changed, 5 insertions, 1 deletion
reduce.nv
@@ -9,6 +9,9 @@ | |||
9 | 9 | ||
10 | 10 | 'snake' x is 20 and y is 3 | |
11 | 11 | ||
12 | + | also note that you can replace the stack name with @main to | |
13 | + | push the result to the main stack . | |
14 | + | ||
12 | 15 | ||
13 | 16 | || '@include' lib/math.nv | |
14 | 17 | ||
@@ -23,6 +26,7 @@ | |||
23 | 26 | | '' (@reduce $word) | '' @reduce 'output' $word | |
24 | 27 | ||
25 | 28 | | '' convert $stack stack to tuple 'destination' $destination | '@js' | |
29 | + | let destination = $destination == "@main" ? "" : $destination; | |
26 | 30 | const tuple = stacks[$stack].join(" "); | |
27 | 31 | delete stacks[$stack]; | |
28 | - | f($destination, tuple); | |
32 | + | f(destination, tuple); |
autumn ревизій цього gist . До ревизії
1 file changed, 28 insertions
reduce.nv(файл створено)
@@ -0,0 +1,28 @@ | |||
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 | + | ||
13 | + | || '@include' lib/math.nv | |
14 | + | ||
15 | + | | '' (@reduce .) | '' convert output stack to tuple | |
16 | + | | '' (@reduce $stack =) | '' @reduce 'destination' $stack | |
17 | + | | '' (@reduce {) '@math' $result | '' (@reduce { $result) | |
18 | + | | '' (@reduce { $result }) | '' @reduce 'output' $result | |
19 | + | | '' (@reduce { $a $b +) | '' (@reduce {) '@math' add $a $b | |
20 | + | | '' (@reduce { $a $b -) | '' (@reduce {) '@math' subtract $a $b | |
21 | + | | '' (@reduce { $a $b *) | '' (@reduce {) '@math' multiply $a $b | |
22 | + | | '' (@reduce { $a $b /) | '' (@reduce {) '@math' divide $a $b | |
23 | + | | '' (@reduce $word) | '' @reduce 'output' $word | |
24 | + | ||
25 | + | | '' convert $stack stack to tuple 'destination' $destination | '@js' | |
26 | + | const tuple = stacks[$stack].join(" "); | |
27 | + | delete stacks[$stack]; | |
28 | + | f($destination, tuple); |