@@ -895,11 +895,39 @@ Combining multiple cycles and C<:partial> also works:
895
895
896
896
See L < this blog post for more elaboration on rotor|http://perl6.party/post/Perl-6-.rotor-The-King-of-List-Manipulation > .
897
897
898
+ = head2 routine cross
899
+
900
+ sub cross(+@e, :&with) returns Seq:D
901
+
902
+ Computes the cross-product of two or more lists or L < iterables|/type/Iterable > .
903
+ This returns a sequence of lists where the first item in each list is an item
904
+ from the first iterable, the second is from the second given iterable, etc.
905
+ Every item will be paired with every other item in all the other lists.
906
+
907
+ say cross(<a b c>, <d e f>).map(*.join).join(",")
908
+ # ad,ae,af,bd,be,bf,cd,ce,cf
909
+
910
+ The C < cross > routine has an infix synonym as well, named C < X > .
911
+
912
+ say (<a b c> X <d e f>).map(*.join).join(",")
913
+ # output is the same as the previous example
914
+
915
+ If the optional C < with > parameter is passed, it is used as a reduction operation
916
+ to apply to each of the cross product items.
917
+
918
+ say cross([1, 2, 3], [4, 5, 6], :with(&infix:<*>)).join(",");
919
+ # 4,5,6,8,10,12,12,15,18
920
+
921
+ The C < X > operator can be combined with another operator as a meta-operator to perform a reduction as well:
922
+
923
+ say ([1, 2, 3] X* [4, 5, 6]).join(",")
924
+ # same output as the previous example
925
+
898
926
= head2 routine zip
899
927
900
928
Defined as:
901
929
902
- sub zip(**@e ) returns Seq:D
930
+ sub zip(+@e, :&with ) returns Seq:D
903
931
904
932
Zips two or more lists or other L < iterables|/type/Iterable > together by
905
933
returning a sequence made of a list of all first elements of all lists, then a
@@ -914,6 +942,19 @@ C<zip> has an infix synonym, the C<Z> operator.
914
942
915
943
say .join for <a b c> Z <d e f>; # same output as above
916
944
945
+ The optional C < with > parameter may be used to reduce the zipped lists. For
946
+ example, the following multiplies each pair to get a result.
947
+
948
+ .say for zip (1, 2, 3), (4, 5, 6), :with(&infix:<*>);
949
+ # 4
950
+ # 10
951
+ # 18
952
+
953
+ The C < Z > form can also be used to perform reduction like the C < with > parameter
954
+ by using it as a meta-operator with the reducing operator:
955
+
956
+ .say for (1, 2, 3) Z* (4, 5, 6); # same output as previous
957
+
917
958
When the first input list is exhausted, no more elements are returned; so
918
959
trailing elements from longer input lists are discarded.
919
960
0 commit comments