Apply a callback to an array: Difference between revisions
Added Odin variant
(Added Odin variant) |
|||
Line 2,344:
(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|Odin}}==
<syntaxhighlight lang="odin">package main
import "core:slice"
import "core:fmt"
squared :: proc(x: int) -> int {
return x * x
}
main :: proc() {
arr := []int{1, 2, 3, 4, 5}
res := slice.mapper(arr, squared)
fmt.println(res) // prints: [1, 4, 9, 16, 25]
}</syntaxhighlight>
=={{header|Oforth}}==
|