Return type of std.algorithm.map

bearophile bearophileHUGS at lycos.com
Thu Jul 7 11:10:51 PDT 2011


James Fisher:

> 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.

Welcome to functional D :-)


> The most general interfaces I can see are InputRange(E) and OutputRange(E).

In D language there is about as much support for OOP as in Java (and more), but in practice OO interfaces are not used significantly in the D standard library. So those are a different kind of "interfaces", they are a bit more like C++0x Concepts, they are very statically typed. So while they are organized in a hierarchy, you can't assign them as OO interfaces.

You generally use type inference to assign the type of map. In functional programming a bit of type inference is not optional, it's almost needed.

map() in D is lazy, it doesn't return an array, but a lazily iterable range. If you want an eager dynamic array you may use array():
array(map!((a) { return a * a; })(start));

If you don't want to use type inference for the return result of map, you have to take a look at the source code of std.functional, read what type is exactly map, and copy it. Generally this is not done in normal user code.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list