Anonymous user
Apply a callback to an array: Difference between revisions
add m4
m (→{{header|Perl}}: migrate content from Loop Structures) |
(add m4) |
||
Line 702:
If you used pairs() instead of ipairs(), this would even work on a hash table in general.
=={{header|M4}}==
<lang M4>
define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')')dnl
define(`_arg1', `$1')dnl
define(`_foreach', `ifelse(`$2', `()', `',
`define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')')dnl
dnl
define(`apply',`foreach(`x',$1,`$2(x)')')dnl
dnl
define(`z',`eval(`$1*2') ')dnl
apply(`(1,2,3)',`z')
</lang>
Output:
<pre>
2 4 6
</pre>
=={{header|Modula-3}}==
|