How to best translate this C++ algorithm into D?
Timon Gehr via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jun 7 00:16:44 PDT 2014
On 06/07/2014 05:50 AM, logicchains wrote:
>
> While my (much more concise; thanks D!) attempt at implementing it is:
>
> 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
> .filter!(a => !forest_invalid(a)).array
> .sort.uniq.array;
> }
>
> Any suggestions for how I could make the D code do the same as the C++
> standard transform? Some sort of flatmap could be useful, to avoid the
> need for a join.
You could use map instead of an explicit array literal doing manual
mapping eagerly and I'm quite confident this accounts for much of the
performance difference.
> It'd also be nice if there was a way to sort/uniq the
> filterresults directly without having to convert to an array first.
You could use 'partition' instead of 'filter'.
More information about the Digitalmars-d
mailing list