for example i have an array
int[] a = [1,2,3,4,5];
and a function
auto twice = function (int x) => x * 2;
how can i apply the function to each element in a without using a
forloop? - is there a function to do this?
a.foreach(x => x * 2);
a == [2,4,6,8,10]
ddos:
auto twice = function (int x) => x * 2;
"function" is not necessary. And generally it's better to assign
to immutables, unless you need to mutate the variable "twice"
later.
how can i apply the function to each element in a without using
a forloop? - is there a function to do this
thx alot! its not important to me that the function is not
evaluated in place
since you gave me such a straight answer i'd like to bother you
with another question :)
for example i have now two ranges:
immutable a = [1,2,3,4];
immutable b = [2,3,4,5];
how do i add the elements in a and b elem
ddos:
how do i add the elements in a and b elementwise
Several ways to do it:
void main() {
import std.range, std.algorithm, std.array;
immutable a = [1, 2, 3, 4];
immutable b = [2, 3, 4, 5];
int[] c1;
c1.reserve(a.length); // Optional.
foreach (immutable x, immutabl