Catamorphism: Difference between revisions
Content added Content deleted
(Add Zig example) |
m (→{{header|RPL}}: formatted code, comment on STREAM function) |
||
Line 2,605: | Line 2,605: | ||
≪ → array op |
≪ → array op |
||
≪ array 1 GET 2 |
≪ array 1 GET 2 |
||
WHILE DUP array SIZE ≤ REPEAT |
'''WHILE''' DUP array SIZE ≤ '''REPEAT''' |
||
array OVER GET ROT SWAP op EVAL |
array OVER GET ROT SWAP op EVAL |
||
SWAP 1 + |
SWAP 1 + |
||
END DROP |
'''END''' DROP |
||
≫ ≫ 'REDUCE' STO |
≫ ≫ '<span style="color:blue">REDUCE</span>' STO |
||
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ + ≫ REDUCE |
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ + ≫ <span style="color:blue">REDUCE</span> |
||
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ - ≫ REDUCE |
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ - ≫ <span style="color:blue">REDUCE</span> |
||
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ * ≫ REDUCE |
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ * ≫ <span style="color:blue">REDUCE</span> |
||
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ MAX ≫ REDUCE |
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ MAX ≫ <span style="color:blue">REDUCE</span> |
||
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ SQ + ≫ REDUCE |
[ 1 2 3 4 5 6 7 8 9 10 ] ≪ SQ + ≫ <span style="color:blue">REDUCE</span> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,624: | Line 2,624: | ||
1: 385 |
1: 385 |
||
</pre> |
</pre> |
||
From HP-48G models, a built-in function named <code>STREAM</code> performs exactly the same as the above <code>REDUCE</code> one, but only with lists. |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
The method inject (and it's alias reduce) can be used in several ways; the simplest is to give a methodname as argument: |
The method inject (and it's alias reduce) can be used in several ways; the simplest is to give a methodname as argument: |