Mailinglist Archive: yast-devel (116 mails)

< Previous Next >
Re: [yast-devel] reduce builtin
  • From: Martin Vidner <mvidner@xxxxxxx>
  • Date: Fri, 20 Jun 2008 13:02:31 +0200
  • Message-id: <20080620110231.GC9224@xxxxxxxxxxxxxxxx>
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.

Please comment.

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
advanced example):

list<string> ls = ["Foo", "bar", "baz"];
integer total_length = list::reduce(integer l, string s, 0, ls, { return l +
size(s); });

(The other variant has no other choice than using a single type,
since it takes a single data argument (which is a list).)

Also, how about some more meaningful names than reduce_1, reduce_2?
Eg. according to the initializer: reduce_internal, reduce_external.

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

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

--
Martin Vidner, YaST developer
http://en.opensuse.org/User:Mvidner

Kuracke oddeleni v restauraci je jako fekalni oddeleni v bazenu
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups
References