On Monday, 15 June 2015 at 16:16:00 UTC, Ali Çehreli wrote:
On 06/15/2015 08:21 AM, Adam D. Ruppe wrote:
> don't call .array if you don't have to, chaining calls to
> map and such, even foreach(item; some_map_result) can be done
without
> actually building the array and can give more efficiency.
To add, the OP can use 'sum' or 'reduce' for "adding them
together":
http://dlang.org/phobos/std_algorithm_iteration.html
import std.stdio;
import std.algorithm;
import std.math;
void main()
{
float[] arr = [ 1.5, 2.5 ];
auto y = arr.map!exp;
writeln(y.sum); // same as sum(y)
}
An equivalent of the last line:
writeln(reduce!((result, a) => result + a)(y));
Ali
`sum` is better for floating-point ranges, because it uses
pair-wise or Kahan summation if possible, in order to preserve
precision.