digitalmars.D.learn - Reduce help..
- Andrej Mitrovic (12/12) Sep 02 2011 string[2][] results;
- Timon Gehr (2/14) Sep 02 2011 len = reduce!max(map!"a[0].length"(results));
- David Nadlinger (7/19) Sep 02 2011 The return type of the function/… passed to reduce must be the same as...
- David Nadlinger (7/9) Sep 02 2011 ´
- David Nadlinger (5/14) Sep 02 2011 … as Vladimir already posted. Gah, I should really refresh d.D.learn
- Vladimir Panteleev (7/19) Sep 02 2011 Here's another way which doesn't use map:
- Andrej Mitrovic (1/1) Sep 02 2011 Thanks guys!
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
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
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
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
On 9/2/11 8:05 PM, David Nadlinger wrote:On 9/2/11 7:23 PM, David Nadlinger wrote:… 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[…] 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
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