To date, I've been using D in much the same way I used C++, without heavy use of templates. Now I'm trying out a more functional style using std.algorithm. However I'm pretty much stuck at the first hurdle: map. With type inference, this works:
import std.algorithm;
import std.stdio;
void main() {
auto start = [1,2,3,4,5];
auto squares = map!((a) { return a * a; })(start);
writeln(squares);
}
Without type inference (obviously necessary beyond trivial examples), it'd be nice to do:
int[] start = [1,2,3,4,5];
int[] squares = map!((a) { return a * a; })(start);
but this gives "Error: cannot implicitly convert expression (map(start)) of type Result to int[]". That opaque type "Result" is weird (not parameterized on "int"?), but OK, let's try that:
int[] start = [1,2,3,4,5];
Result squares = map!((a) { return a * a; })(start);
gives "undefined identifier Result". Not sure why, but OK. I can't see examples in the docs of explicit type declarations -- annoyingly they all use "auto".
However, they do tell me that map "returns a range". Assuming all definitions of ranges are in std.range, there's no such thing as a "Range" interface, so it's not that. The most general interfaces I can see are InputRange(E) and OutputRange(E). It certainly can't be an OutputRange, so I guess it must satisfy InputRange, presumably type-parameterized with "int". So this should work:
int[] start = [1,2,3,4,5];
InputRange!int squares = map!((a) { return a * a; })(start);
But the compiler complains "cannot implicitly convert expression (map(start)) of type Result to std.range.InputRange!(int).InputRange". That's weird, because "std.range.InputRange!(int).InputRange" doesn't even look like a type.
I've tried all manner of combinations here, but nothing works. Seems like there's something fundamental I'm not understanding.