How to best translate this C++ algorithm into D?

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 7 01:21:46 PDT 2014


On Saturday, 7 June 2014 at 03:50:17 UTC, logicchains wrote:
>       std::transform(std::begin(forests), std::end(forests),
> 		     std::back_inserter(next_forests),
> 		     [&](const forest_t& forest) {
> 		       std::transform(std::begin(forest), std::end(forest),
> 				      std::begin(meal), std::begin(next_forest), 
> std::plus<int>());
> 		       return next_forest;
> 		     });
>     }

You translate this chunk into:

> forest_t[] meal(in forest_t[] forests) {
>   forest_t[3] possible_meals = [{-1, -1, +1}, {-1, +1, -1}, 
> {+1, -1, -1}];
>   return map!(a => [possible_meals[0]+a, possible_meals[1]+a, 
> possible_meals[2]+a])(forests)
>     .join

First, "[possible_meals[0]+a, ...]" will allocate a new slice 
*every* *single* *time*. Also, you probably want "joiner" rather 
than straight up "join". Although "join|er" as often proved slow 
in benchmarks.

I have to run, so I can't suggest what to use instead, but I 
still thought I'd point it out.


More information about the Digitalmars-d mailing list