Thread overview
Reduce help..
Sep 02, 2011
Andrej Mitrovic
Sep 02, 2011
Timon Gehr
Sep 02, 2011
David Nadlinger
Sep 02, 2011
David Nadlinger
Sep 02, 2011
David Nadlinger
Sep 02, 2011
Vladimir Panteleev
Sep 02, 2011
Andrej Mitrovic
September 02, 2011
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?
September 02, 2011
On 09/02/2011 07: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?


len = reduce!max(map!"a[0].length"(results));
September 02, 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
September 02, 2011
On Fri, 02 Sep 2011 20:11:38 +0300, Andrej Mitrovic <andrej.mitrovich@gmail.com> 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?

Here's another way which doesn't use map:

len = reduce!`max(a, b[0].length)`(0, results);

-- 
Best regards,
 Vladimir                            mailto:vladimir@thecybershadow.net
September 02, 2011
Thanks guys!
September 02, 2011
On 9/2/11 7:23 PM, David Nadlinger wrote:
> […]  because reduce is really just another way to express
> the above loop.
´
On second thought: The one-argument overload of it, that is. You can also use differing types if you explicitly specify the starting value. For your example you could do something like:
reduce!"max(a, b[0].length)"(0, …).

David
September 02, 2011
On 9/2/11 8:05 PM, David Nadlinger wrote:
> On 9/2/11 7:23 PM, David Nadlinger wrote:
>> […] because reduce is really just another way to express
>> the above loop.
> ´
> On second thought: The one-argument overload of it, that is. You can
> also use differing types if you explicitly specify the starting value.
> For your example you could do something like:
> reduce!"max(a, b[0].length)"(0, …).
>
> David

… as Vladimir already posted. Gah, I should really refresh d.D.learn before posting, it's quite impressive how fast simple questions are often answered here…

David