range foreach lambda

2014-07-17 Thread ddos via Digitalmars-d-learn
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]

Re: range foreach lambda

2014-07-17 Thread bearophile via Digitalmars-d-learn
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

Re: range foreach lambda

2014-07-17 Thread ddos via Digitalmars-d-learn
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

Re: range foreach lambda

2014-07-17 Thread bearophile via Digitalmars-d-learn
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