Reduce help..

David Nadlinger see at klickverbot.at
Fri Sep 2 10:23:03 PDT 2011


On 9/2/11 7:11 PM, Andrej Mitrovic wrote:
> string[2][] results;
> results ~= ["foo", ""];
> results ~= ["foobar", ""];
>
> size_t len;
> foreach (res; results)
> {
>      len = max(len, res[0].length);
> }
>
> That gives me '6'. I want to convert this to functional-style code
> with reduce. I've tried:
>
> len = reduce!(max!"a[0].length")(results);
>
> That's not it. Any clues?

The return type of the function/… passed to reduce must be the same as 
its argument type, because reduce is really just another way to express 
the above loop.

In this case, you might want to combine map and reduce:
reduce!max(map!"a[0].length"(results))

David


More information about the Digitalmars-d-learn mailing list