Apply a callback to an array: Difference between revisions

added java maybe
(added java maybe)
Line 407:
 
will yield a scalar if <tt>a</tt> is scalar or a vector if <tt>a</tt> is a vector or an n-dimensional array if <tt>a</tt> is an n-dimensional array
 
=={{header|Java}}==
 
As of the current version of Java, you have to define an interface for each type of function you want to use. The next version of Java will introduce function types.
 
So if you want to perform an action (which doesn't return anything) on an array of int's:
 
<java>interface IntToVoid {
void run(int x);
}
 
for (int z : myIntArray) {
new IntToVoid() {
public void run(int x) {
System.out.println(x);
}
}.run(z);
}</java>
 
Or if you want to perform "map" - return an array of the results of function applications:
 
<java>interface IntToInt {
int run(int x);
}
 
int[] result = new int[myIntArray.length];
for (int i = 0; i < myIntArray.length; i++) {
result[i] =
new IntToInt() {
public int run(int x) {
return x * x;
}
}.run(myIntArray[i]);
}</java>
 
=={{header|JavaScript}}==
Anonymous user