Skip to content

Commit b4d4dee

Browse files
committed
Merge pull request #424 from MadcapJake/master
Add, fix and reword feed operator docs
2 parents 5ac95ba + 7ac403e commit b4d4dee

File tree

1 file changed

+89
-26
lines changed

1 file changed

+89
-26
lines changed

doc/Language/operators.pod

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,10 +1897,14 @@ Short-circuits.
18971897
18981898
=head2 infix C«==>»
18991899
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.
1902+
1903+
The precedence is very loose so you will need to use parentheses to
1904+
assign the result or you can even just use another feed operator! In the
1905+
case of routines/methods that take a single argument or the first argument
1906+
is a block, it is often required that you call with parentheses (though this
1907+
is not required for the very last routine/method).
19041908
19051909
# Traditional structure, read bottom-to-top
19061910
my @result =
@@ -1909,40 +1913,99 @@ methods of the data in each step.
19091913
map { .tc }, # (2) Capitalize the words
19101914
<people of earth>; # (1) Start with the input
19111915
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>
1916+
# Feed (left-to-right) with parentheses, read top-to-bottom
1917+
my @result = (
1918+
<people of earth> # (1) Start with the input
1919+
==> map({ .tc }) # (2) Capitalize the words
1920+
==> grep /<[PE]>/ # (3) Look for P or E
1921+
==> sort # (4) Sort, result is <Earth People>
1922+
);
19181923
1919-
# For illustration, method chaning equivalent for Lists, read top-to-bottom
1924+
# For illustration, method chaining equivalent, read top-to-bottom
19201925
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>
1926+
<people of earth> # (1) Start with the input
1927+
.map({ .tc }) # (2) Capitalize the words
1928+
.grep(/<[PE]>/) # (3) Look for P or E
1929+
.sort; # (4) Sort, result is <Earth People>
1930+
1931+
# To assign without the need of parentheses use another feed operator
1932+
<people of earth>
1933+
==> map({ .tc })
1934+
==> grep /<[PE]>/
1935+
==> sort()
1936+
==> my @result;
1937+
1938+
# It can be useful to capture a partial result, however, unlike
1939+
# the leftward feed operator, it does require parentheses or a semicolon
1940+
<people of earth>
1941+
==> map({ .tc })
1942+
==> my @caps; @caps # also could wrap in parentheses instead
1943+
==> grep /<[PE]>/
1944+
==> sort()
1945+
==> my @result;
1946+
1947+
The feed operator lets you construct method-chaining-like patterns out of
1948+
routines and the results of methods on unrelated data. In method-chaining,
1949+
you are restricted to the methods available on the data or the result of
1950+
previous method call. With feed operators, that restriction is gone.
1951+
The resulting code could also be seen to be more readable than a series of
1952+
method calls broken over multiple lines.
1953+
1954+
Note: In the future, this operator will see some change as it gains the
1955+
ability to run list operations in parallel. It will enforce that the
1956+
B<left> operand is enclosable as a closure (that can be cloned and run in
1957+
a subthread).
19251958
19261959
=head2 infix C«<==»
19271960
19281961
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.
1962+
it to the previous (left) routine as the last parameter. This
1963+
elucidates the right-to-left dataflow for a series of list manipulating
1964+
functions.
19311965
19321966
# 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
19401967
my @result =
19411968
sort # (4) Sort, result is <Earth People>
1942-
<== grep { /<[PE]>/ }, # (3) Look for P or E
1943-
<== map { .tc }, # (2) Capitalize the words
1969+
grep { /<[PE]>/ }, # (3) Look for P or E
1970+
map { .tc }, # (2) Capitalize the words
1971+
<people of earth>; # (1) Start with the input
1972+
1973+
# Feed (right-to-left) with parentheses, read bottom-to-top
1974+
my @result = (
1975+
sort() # (4) Sort, result is <Earth People>
1976+
<== grep({ /<[PE]>/ }) # (3) Look for P or E
1977+
<== map({ .tc }) # (2) Capitalize the words
1978+
<== <people of earth> # (1) Start with the input
1979+
);
1980+
1981+
# To assign without parentheses, use another feed operator
1982+
my @result
1983+
<== sort() # (4) Sort, result is <Earth People>
1984+
<== grep({ /<[PE]>/ }) # (3) Look for P or E
1985+
<== map({ .tc }) # (2) Capitalize the words
19441986
<== <people of earth>; # (1) Start with the input
19451987
1988+
# It can be useful to capture a partial result
1989+
my @result
1990+
<== sort()
1991+
<== grep({ /<[PE]>/ })
1992+
<== my @caps # unlike <==, there is no need for addtl. statement
1993+
<== map({ .tc })
1994+
<== <people of earth>;
1995+
1996+
Unlike the rightward feed operator, the result is not closely mappable
1997+
to method-chaining. However, compared to the traditional structure above
1998+
where each argument is separated by a line, the resulting code is more
1999+
demonstrative than commas. The leftward feed operator also allows you
2000+
to "break into" the statement and capture an intermediary result which
2001+
can be extremely useful for debugging or to take that result and create
2002+
another variation on the final result.
2003+
2004+
Note: In the future, this operator will see some change as it gains the
2005+
ability to run list operations in parallel. It will enforce that the
2006+
B<right> operand is enclosable as a closure (that can be cloned and run in
2007+
a subthread).
2008+
19462009
=end pod
19472010

19482011
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)