Casting MapResult
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 15 09:15:59 PDT 2015
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
More information about the Digitalmars-d-learn
mailing list