@@ -929,59 +929,91 @@ Defined as:
929
929
930
930
sub zip(+@e, :&with) returns Seq:D
931
931
932
- Zips two or more lists or other L < iterables|/type/Iterable > together by
933
- returning a sequence made of a list of all first elements of all lists, then a
934
- list of all second elements of a list etc.
932
+ Builds a 'list of lists', returned as a sequence, from multiple input lists or
933
+ other L < iterables|/type/Iterable > .
935
934
936
- say .join for zip <a b c>, <d e f>;
937
- # ad
938
- # be
939
- # cf
935
+ C < zip > iterates through each of the input lists synchronously, 'Zipping' them
936
+ together, so that elements are grouped according to their input list index, in
937
+ the order that the lists are provided.
938
+
939
+ say zip(<a b c>, <d e f>, <g h i>);
940
+ # ((a d g) (b e h) (c f i))
940
941
941
942
C < zip > has an infix synonym, the C < Z > operator.
942
943
943
- say .join for <a b c> Z <d e f>; # same output as above
944
+ say <a b c> Z <d e f> Z <g h i> ; # same output
944
945
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.
946
+ C < zip > can provide input to a for loop :
947
947
948
- .say for zip (1, 2, 3), (4, 5, 6), :with(&infix:<*>);
949
- # 4
950
- # 10
951
- # 18
948
+ for <a b c> Z <d e f> Z <g h i> -> [$x,$y,$z] {say ($x,$y,$z).join(",")}
949
+ # a,d,g
950
+ # b,e,h
951
+ # c,f,i
952
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:
953
+ , or more succinctly :
955
954
956
- .say for (1, 2, 3) Z* (4, 5, 6); # same output as previous
955
+ say .join(",") for zip <a b c>, <d e f>, <g h i>; # same output
956
+
957
+ Note, that if the input lists have an unequal number of elements, then
958
+ C < zip > terminates once the shortest input list is exhausted, and trailing
959
+ elements from longer input lists are discarded.
957
960
958
- When the first input list is exhausted, no more elements are returned; so
959
- trailing elements from longer input lists are discarded.
961
+ say <a b c> Z <d e f m n o p> Z <g h i>;
962
+ # ((a d g) (b e h) (c f i))
960
963
961
- If you just wish to skip missing entries in shorter sublists,
962
- use L < roundrobin|/type/List#sub_roundrobin > instead:
964
+ In cases where data clipping is possible, but undesired, then consider using
965
+ L < roundrobin|/type/List#sub_roundrobin > instead of C < zip > .
963
966
964
- = for code :skip-test
965
- for roundrobin(@queue1, @queue2, @queue3) -> $next {
966
- ...
967
- }
967
+ The optional C < with > parameter will additionally reduce the zipped lists. For
968
+ example, the following multiplies corresponding elements together to return a
969
+ single list of products.
970
+
971
+ .say for zip <1 2 3>, [1, 2, 3], (1, 2, 3), :with(&infix:<*>);
972
+ # 1
973
+ # 8
974
+ # 27
975
+
976
+ The C < Z > form can also be used to perform reduction by implicitly setting the
977
+ C < with > parameter with a meta-operator :
978
+
979
+ .say for <1 2 3> Z* [1, 2, 3] Z* (1, 2, 3); # same output
968
980
969
981
= head2 sub roundrobin
970
982
971
983
Defined as:
972
984
973
985
method roundrobin(List:D:) returns Seq
974
986
975
- C < roundrobin > is very similar to L < zip|/type/List#routine_zip > . The
976
- difference is that C < roundrobin > will not stop on lists that run out of
977
- elements but simply skip any undefined value:
987
+ Builds a 'list of lists', returned as a sequence, from multiple input lists or
988
+ other L < iterables|/type/Iterable > . C < roundrobin > returns an identical result to
989
+ that of L < zip|/type/List#routine_zip > , except when the input lists have an
990
+ unequal number of elements.
991
+
992
+ say roundrobin <a b c>, <d e f>, <g h i>;
993
+ # ((a d g) (b e h) (c f i))
994
+
995
+ say .join(",") for roundrobin([1, 2], [2, 3], [3, 4]);
996
+ # 1,2,3
997
+ # 2,3,4
998
+
999
+ C < roundrobin > does not terminate once one or more of the input lists become
1000
+ exhausted, but proceeds until all elements from all lists have been processed.
1001
+
1002
+ say roundrobin <a b c>, <d e f m n o p>, <g h i j>;
1003
+ # ((a d g) (b e h) (c f i) (m j) (n) (o) (p))
978
1004
979
- my @a = 1;
980
- my @b = 1..2;
981
- my @c = 1..3;
982
- for flat roundrobin(@a, @b, @c) -> $x { $x.say }
1005
+ say .join(",") for roundrobin([1, 2], [2, 3, 57, 77], [3, 4, 102]);
1006
+ # 1,2,3
1007
+ # 2,3,4
1008
+ # 57,102
1009
+ # 77
1010
+
1011
+ Therefore no data values are lost due in the 'zipping' operation. A record of
1012
+ which input list provided which element cannot be gleaned from the resulting
1013
+ sequence, however.
983
1014
984
- will display the following values: C < 1, 1, 1, 2, 2, 3 >
1015
+ C < roundrobin > can be useful in combining messy data to the point where a manual
1016
+ post-processing step can then be undertaken.
985
1017
986
1018
= head2 routine sum
987
1019
0 commit comments