Another article of the series “Yet Another Perl 6 Operator”
In a previous article , we introduced the reduction operators (like '[*]' and '[~]') which produced list operators from infix operators (like '*' and '~').
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, '[\+]') 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 Perl 6 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 mkpath function (similar to the one provided by the Perl 5 module File::Path ) with a mkdir builtin.
sub mkpath (Str $path) {
# split 'a/b/c' into ( 'a/', 'b/', 'c' )
my @segments = $path.split( /<!after '/'>/ );
for =[\~] @segments {
# path exists as dir or create it
.:d || .mkdir;
}
}
This will create the directories 'a/', 'a/b/', and 'a/b/c' if they do not exist yet.
Next article is due next Thursday (Jan 3, 2008).


Unfortunately there are a bunch of backslashes missing from the html-ified article. Looks like all but the "expanded" equivalent should have the backslash inside the opening square bracket of the reduction operator.
It should be fixed now. And that's another fight I lose for O'Reilly MT. I haven't see no issue when previewing the article, but after you told it, I noticed that inside <pre> blocks, the backslashes needed to be repeated to appear.
a previous article link is wrong
Hmm.... semantically almost equivalent to the '/' (reduce) operator from APL (1968).
In APL, "+/a" applies "+ reduce" to vector "a" yielding a scalar result. Any infix operator could appear in front of the "/".
It's all the same stuff, rediscovered every couple of decades by a new generation of developers (viz. Virtualization, also from the '60s)
Previous article link is still broken, it should be:
http://www.oreillynet.com/onlamp/blog/2007/12/yap6_operator_reduce_operators.html
@rahed @Andy Bach: fixed the "previous article" link. Thanks.