# Mailinglist Archive: yast-devel (116 mails)

 < Previous Next >
##### Re: [yast-devel] reduce builtin
• From: Arvin Schnell <aschnell@xxxxxxx>
• Date: Fri, 20 Jun 2008 15:12:50 +0200
• Message-id: <20080620131250.GA5593@xxxxxxx>
On Fri, Jun 20, 2008 at 01:02:31PM +0200, Martin Vidner wrote:
On Fri, Jun 20, 2008 at 10:01:16AM +0200, Arvin Schnell wrote:
it's ITO Friday again. I still have an improved patch for the reduce
function (http://en.wikipedia.org/wiki/Reduce_%28higher-order_function%29)
lying around (see attachment). It adds two versions of the function:

1) One that takes an initial value.

integer sum = list::reduce(integer x, integer y, 0, [ 1, 2, 3 ], {
return x + y; });
integer product = list::reduce(integer x, integer y, 1, [ 1, 2, 3 ], {
return x * y; })

2) One were the initial value is the value of the first list entry. Here
the list must of course have at least one entry.

integer min = list::reduce(integer x, integer y, [ 1, 2, 3 ], { return
x < y ? x : y; });
integer max = list::reduce(integer x, integer y, [ 1, 2, 3 ], { return
x > y ? x : y; });

The behaviour is now analogous to Python's reduce so it shouldn't be
completely screwed. The patch contains no binary incompatibility to libycp
or the bytecode.

Hey, that's nice.

I notice that the variant with an external initializer uses 2
different types. That is worth pointing out explicitly (as an

Sure, I'll mention it in the docu.

Hmm, would it make sense to use "list::shift" and implement
reduce_internal using reduce_external?

Maybe, but we would require list::shift in C++. Since it
wouldn't affect the YCP interface it can always be improved.

If noone objects, the next steps would be completing the docs
(mainly examples, corner cases), adding test cases, svn, autobuild.

Yes, I'll do so.

ciao Arvin
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx