Apply a callback to an array: Difference between revisions
Content added Content deleted
(Modula-3) |
(→{{header|FP}}: ++ gnu octave) |
||
Line 480: | Line 480: | ||
{square * . [id, id]} |
{square * . [id, id]} |
||
& square: <1,2,3,4,5> |
& square: <1,2,3,4,5> |
||
=={{header|GNU Octave}}== |
|||
Almost all the built-in can operate on each element of a vector or matrix; e.g. sin([pi/2, pi, 2*pi]) computes the function sin on pi/2, pi and 2*pi (returning a vector). If a function does not accept vectors/matrices as arguments, the <tt>arrayfun</tt> can be used. |
|||
<lang octave>function e = f(x, y) |
|||
e = x^2 + exp(-1/(y+1)); |
|||
endfunction |
|||
% f([2,3], [1,4]) gives and error, but |
|||
arrayfun(@f, [2, 3], [1,4]) |
|||
% works</lang> |
|||
(The function <tt>f</tt> can be rewritten so that it can accept vectors as argument simply changing operators to their dot ''relatives'': <code>e = x.^2 + exp(-1 ./ (y.+1))</code>) |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |