Apply a callback to an array: Difference between revisions
Content added Content deleted
(→{{header|Erlang}}: -- Added Euphoria code + explanation) |
|||
Line 447: | Line 447: | ||
6 |
6 |
||
</lang> |
</lang> |
||
=={{header|Euphoria}}== |
|||
<lang euphoria>function apply_to_all(sequence s, integer f) |
|||
-- apply a function to all elements of a sequence |
|||
sequence result |
|||
result = {} |
|||
for i = 1 to length(s) do |
|||
-- we can call add1() here although it comes later in the program |
|||
result = append(result, call_func(f, {s[i]})) |
|||
end for |
|||
return result |
|||
end function |
|||
function add1(atom x) |
|||
return x + 1 |
|||
end function |
|||
-- add1() is visible here, so we can ask for its routine id |
|||
? apply_to_all({1, 2, 3}, routine_id("add1")) |
|||
-- displays {2,3,4}</lang> |
|||
This is also "Example 2" in the Euphoria documentation for <code>routine_id()</code>. |
|||
Note that this example will not work for multi-dimensional sequences. |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |