www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reduce help..

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Sep 02 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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));
Sep 02 2011
prev sibling next sibling parent reply David Nadlinger <see klickverbot.at> writes:
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
Sep 02 2011
parent reply David Nadlinger <see klickverbot.at> writes:
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
Sep 02 2011
parent David Nadlinger <see klickverbot.at> writes:
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
Sep 02 2011
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
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
Sep 02 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Thanks guys!
Sep 02 2011