Apply a callback to an array: Difference between revisions

no edit summary
(→‎{{header|Python}}: print squares of integers in the range from 0 to 9)
No edit summary
Line 284:
 
=={{header|D}}==
 
===Using foreach===
 
// In-place:
auto data = [1, 3, 5, 7];
Line 294 ⟶ 297:
foreach (i, el; data)
data2[i] = el*el;
===Using a function===
 
U[] map(T, U)(T[] array, U delegate(T) dg)
{
auto result = new U[array.length];
foreach (index, element; array)
result[index] = dg(element);
return result;
}
void main()
{
writefln(
[1, 2, 3, 4, 5].map(
(int i) { return i+5; }
)
);
}
 
=={{header|E}}==
Anonymous user