How do I pass an unflattened array repetition to a reduce in Perl 6?
arrays, perl, raku
Solution
Assuming you also want your code to work with arrays repeated more than twice, the workaround I came up with reads
(<1 2> xx 2).tree.reduce({ @^a X~ @^b })
Note that flattening behaviour and the List/Parcel distinction1 are known pain points.
A plan to improve the situation has been around for a while (cf Great List Refactor) and is supposed to land before the 6.0 release at the end of the year.
1 All three expressions you looked at have different types:
(<1 2>, <1 2>).WHAT # (Parcel)
(<1 2> xx 2).WHAT # (List)
(<1 2> xx 2).tree.WHAT # (LoL)
The last one is a List-of-Lists.
Problem
I'm trying to pass an xx repeated array to an [X~] reduce function but finding that the array gets flattened. I've searched the online docs and stack overflow, but couldn't find anything. Unfortunately, my Perl 6 knowledge is quite rudimentary (though I do know Perl 5 quite well). ``` [X~](<1 2>, <1 2>) # 11 12 21 22 (WHAT I WANT) [X~](<1 2> xx 2) # 1 2 1 2 (NOT WHAT I WANT) [X~](<1 2> xx 2).tree # 1 2 1 2 [X~](<1 2>.tree xx 2) # 1 2 1 2 ``` Looking at the .perl dumps doesn't enlighten me much: ``` (<1 2>, <1 2>).perl # (("1", "2"), ("1", "2")) (<1 2> xx 2).perl # (("1", "2"), ("1", "2")).list (<1 2> xx 2).tree.perl # ("1", "2"; "1", "2").item ``` I'm using rakudo-star-2014.12.1-parrot.msi.