Anonymous user
Apply a callback to an array: Difference between revisions
m
→{{header|R}}: Further examples
(added PowerShell) |
m (→{{header|R}}: Further examples) |
||
Line 955:
=={{header|R}}==
Many functions can take advantage of implicit vectorisation, e.g.
<lang R>cube <- function(x) x*x*x▼
<lang R>
elements <- c(1, 2, 3, 4)▼
cubes <- cube(elements)
</lang>
Explicit looping over array elements is also possible.
<lang R>
cubes <- numeric(5)
for(i in seq_along(cubes))
{
cubes[i] <- cube(elements[i])
}
</lang>
Loop syntax can often simplified using the [http://stat.ethz.ch/R-manual/R-patched/library/base/html/apply.html *array] family of functions.
<lang R>
elements2 <- list(1,2,3,4,5)
cubes <- sapply(elements2, cube)
</lang>
In each case above, the value of 'cubes' is
1 8 27 64 125
=={{header|Raven}}==
|