@@ -1897,10 +1897,17 @@ Short-circuits.
1897
1897
1898
1898
= head2 infix C « ==> »
1899
1899
1900
- This X < feed > operator takes the result from the left and passes it as the
1901
- last parameter to the right. This allows for a more natural left-to-right data
1902
- flow similar to method chaining, but can be used with functions that are not
1903
- methods of the data in each step.
1900
+ This X < feed > operator takes the result from the left and passes it to the
1901
+ next (right) routine as the last parameter. This allows
1902
+ for a more natural left-to-right data flow similar to method chaining, but
1903
+ gives you the freedom to use something other than methods directly available
1904
+ to the data (the result of a bare routine or an unrelated data's method).
1905
+
1906
+ The precedence is very loose so you will need to use parentheses to
1907
+ assign the result or you can even just use another feed operator! In the
1908
+ case of routines/methods that take a single argument or the first argument
1909
+ is a block, it is often required that you call with parentheses (though this
1910
+ is not required for the very last routine/method).
1904
1911
1905
1912
# Traditional structure, read bottom-to-top
1906
1913
my @result =
@@ -1909,40 +1916,74 @@ methods of the data in each step.
1909
1916
map { .tc }, # (2) Capitalize the words
1910
1917
<people of earth>; # (1) Start with the input
1911
1918
1912
- # Feed (left-to-right), read top-to-bottom
1913
- my @result =
1914
- <people of earth> # (1) Start with the input
1915
- ==> map { .tc } # (2) Capitalize the words
1916
- ==> grep /<[PE]>/ # (3) Look for P or E
1917
- ==> sort; # (4) Sort, result is <Earth People>
1919
+ # Feed (left-to-right) with parentheses, read top-to-bottom
1920
+ my @result = (
1921
+ <people of earth> # (1) Start with the input
1922
+ ==> map({ .tc }) # (2) Capitalize the words
1923
+ ==> grep /<[PE]>/ # (3) Look for P or E
1924
+ ==> sort # (4) Sort, result is <Earth People>
1925
+ );
1918
1926
1919
- # For illustration, method chaning equivalent for Lists , read top-to-bottom
1927
+ # For illustration, method chaining equivalent, read top-to-bottom
1920
1928
my @result =
1921
- <people of earth> # (1) Start with the input
1922
- .map({ .tc }) # (2) Capitalize the words
1923
- .grep(/<[PE]>/) # (3) Look for P or E
1924
- .sort; # (4) Sort, result is <Earth People>
1929
+ <people of earth> # (1) Start with the input
1930
+ .map({ .tc }) # (2) Capitalize the words
1931
+ .grep(/<[PE]>/) # (3) Look for P or E
1932
+ .sort; # (4) Sort, result is <Earth People>
1933
+
1934
+ # To assign without the need of parentheses use another feed operator
1935
+ <people of earth>
1936
+ ==> map({ .tc })
1937
+ ==> grep /<[PE]>/
1938
+ ==> sort()
1939
+ ==> my @result;
1940
+
1941
+ # It can be useful to capture a partial result
1942
+ <people of earth>
1943
+ ==> map({ .tc })
1944
+ ==> my @caps; @caps # also could wrap in parentheses instead
1945
+ ==> grep /<[PE]>/
1946
+ ==> sort()
1947
+ ==> my @result;
1925
1948
1926
1949
= head2 infix C « <== »
1927
1950
1928
1951
This X < leftward feed > operator takes the result from the right and passes
1929
- it as the last parameter to the left. This makes visible the right-to-left
1930
- dataflow for a series of list manipulating functions.
1952
+ it to the previous (left) routine as the last parameter. This
1953
+ elucidates the right-to-left dataflow for a series of list manipulating
1954
+ functions.
1931
1955
1932
1956
# Traditional structure, read bottom-to-top
1933
- my @result =
1934
- sort # (4) Sort, result is <Earth People>
1935
- grep { /<[PE]>/ }, # (3) Look for P or E
1936
- map { .tc }, # (2) Capitalize the words
1937
- <people of earth>; # (1) Start with the input
1938
-
1939
- # Feed (right-to-left), read bottom-to-top
1940
1957
my @result =
1941
1958
sort # (4) Sort, result is <Earth People>
1942
- <== grep { /<[PE]>/ }, # (3) Look for P or E
1943
- <== map { .tc }, # (2) Capitalize the words
1959
+ grep { /<[PE]>/ }, # (3) Look for P or E
1960
+ map { .tc }, # (2) Capitalize the words
1961
+ <people of earth>; # (1) Start with the input
1962
+
1963
+ # Feed (right-to-left) with parentheses, read bottom-to-top
1964
+ my @result = (
1965
+ sort() # (4) Sort, result is <Earth People>
1966
+ <== grep({ /<[PE]>/ }) # (3) Look for P or E
1967
+ <== map({ .tc }) # (2) Capitalize the words
1968
+ <== <people of earth> # (1) Start with the input
1969
+ );
1970
+
1971
+ # To assign without parentheses, use another feed operator
1972
+ my @result
1973
+ <== sort() # (4) Sort, result is <Earth People>
1974
+ <== grep({ /<[PE]>/ }) # (3) Look for P or E
1975
+ <== map({ .tc }) # (2) Capitalize the words
1944
1976
<== <people of earth>; # (1) Start with the input
1945
1977
1978
+ # It can be useful to capture a partial result
1979
+ my @result
1980
+ <== sort()
1981
+ <== grep({ /<[PE]>/ })
1982
+ <== my @caps # unlike <==, there is no need for addtl. statement
1983
+ <== map({ .tc })
1984
+ <== <people of earth>;
1985
+
1986
+
1946
1987
= end pod
1947
1988
1948
1989
# vim: expandtab shiftwidth=4 ft=perl6
0 commit comments