=NAME Yet Another S Operator: Reduce Operators - Part II =VERSION Maintainer: Adriano Ferreira Date: 28 Dec 2007 Last Modified: 28 Dec 2007 Number: 17 Version: 1 Status: Draft =head1 Body In L< a previous article | reduce1.html >, we introduced the reduction operators (like C<'[*]'> and C<'[~]'>) which produced list operators from infix operators (like C<'*'> and C<'~'>). There is a variant of the reduction operator that operates over its list argument producing all intermediate results along with the final result of the ordinary reduction. [\+] 1..5 # (1, 3, 6, 10, 15) which is equivalent to ([+] 1), ([+] 1, 2), ([+] 1, 2, 3), ([+] 1, 2, 3, 4), ([+] 1, 2, 3, 4, 5) The above decomposition illustrates that the visual picture of a triangle in the reduce operator (eg, C<'[\+]'>) is not accidental. The triangular reduction lazily generates its resulting list, so that it can be applied to infinite lists. [\*] 1..* # (1, 2, 6, 24, ...) As many other S operators, this one has a functional flavor which may estimulate some Haskellish solutions to some problems. A (rather contrived) application example is to implement a C function (similar to the one provided by the S module L< C | http://search.cpan.org/perldoc?File::Path >) with a C builtin. =begin code sub mkpath (Str $path) { # split 'a/b/c' into ( 'a/', 'b/', 'c' ) my @segments = $path.split( // ); for =[\~] @segments { # path exists as dir or create it .:d || .mkdir; } } =end code This will create the directories C<'a/'>, C<'a/b/'>, and C<'a/b/c'> if they do not exist yet. =SEEALSO L< Reduce Operators - Part I | reduce1.html > $Revision$ =begin comment Keywords: Perl Perl 6 operators reduce operator reduction operator meta-operator =end comment