# New Ticket Created by Stephen Mosher # Please include the string: [perl #78626] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=78626 >
Hi, Attached is a diff against rakudo/src/core/Any-list.pm which adds support to reduce() for higher-arity functions. It ensures arity-list agreement and is generally safe. Other than the test, the functionality was already present... it needed two lines commented out, two uncommented. Since tests passed and there were no explanatory comments in the file, jnthn suggested I mail in a diff. Here it is. I was considering including another patch to support functions that reduce to more than a single term, but as it turns out this is best handled by using a [list] item instead. Note: if the supplied function _does_ return more than a single term, reduce() will not produce a correct result (neither this version nor the original), but it will terminate assuming a finite list. Regards, Stephen
--- Any-list.pm 2010-10-18 21:41:03.000000000 -0400 +++ Any-list.morearity.pm 2010-10-18 21:42:08.000000000 -0400 @@ -218,15 +218,15 @@ my $arity = $expression.?count || 2; # second half is a CHEAT fail('Cannot reduce() using a unary or nullary function.') if $arity < 2; - fail('Can only reduce() using a binary function for now.') - if $arity > 2; + warn('Arity must agree with list length in reduce().') + unless (@.elems -1) %% ($arity -1); my @args = (); for @.list { @args.push($_); if (@args == $arity) { - my $res = $expression.(@args[0], @args[1]); - # my $res = $expression.(|@args); + # my $res = $expression.(@args[0], @args[1]); + my $res = $expression.(|@args); @args = ($res); } } @@ -237,8 +237,8 @@ if @args < $expression.arity { warn (@args -1) ~ " trailing item(s) in reduce"; } else { - return $( $expression.(@args[0], @args[1]) ); - # return $( $expression.(|@args) ); + # return $( $expression.(@args[0], @args[1]) ); + return $( $expression.(|@args) ); } } return @args[0];