how to return map(fn)

Ali Çehreli acehreli at yahoo.com
Mon Feb 21 23:07:44 UTC 2022


On 2/21/22 12:44, steve wrote:

 > I had a look at the source code for map but it seems to return a private
 > struct, which doesn't help.

What map and other Phobos algorithms return are called Voldemort types 
for the reason you state: They are unmentionable. The usual way is to 
return 'auto' and the template system will take care of the rest:

auto mapped(alias f, Range) (Range range) {
   import std.algorithm : map;
   return range.map!f();
}

void main() {
   auto r = [1, 2].mapped!(i => 10 * i);

   import std.algorithm;
   assert(r.equal([10, 20]));
}

And I am sure your function is more capable than being the equivalent as 
map. ;)

Ali



More information about the Digitalmars-d-learn mailing list