Skip to content

Commit 6e3763b

Browse files
committed
Changed some examples to a case that feels a bit more real
And shows intent refs #1748. Also separates code in different examples, leaving the text as text, so it addresses 4 and 5. It closes Still it should also show an example where the method chaining does not work by using some data structure that can be used as the last argument to something, but does not have that something as a method.
1 parent fcd0359 commit 6e3763b

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

doc/Language/operators.pod6

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,38 +2490,46 @@ case of routines/methods that take a single argument or where the first argument
24902490
is a block, it's often required that you call with parentheses (though this
24912491
is not required for the very last routine/method).
24922492
2493-
# Traditional structure, read bottom-to-top
2494-
my @result =
2495-
sort # (4) Sort, result is <Earth People>
2496-
grep { /<[PE]>/ }, # (3) Look for P or E
2497-
map { .tc }, # (2) Capitalize the words
2498-
<people of earth>; # (1) Start with the input
2493+
This "traditional" structure, read bottom-to-top, with the last two
2494+
lines creating the data structure that is going to be processed
2495+
2496+
my @fractions = <TWO THREE FOUR FIVE SEVEN> »~» " " X~ <FIFTHS SIXTHS EIGHTHS>;
2497+
my @result = map { .uniparse }, # (3) Converts to unicode
2498+
grep { .uniparse }, # (2) Checks if it parses
2499+
map( {"VULGAR FRACTION " ~ $^þ }, @fractions); # (1) Adds string to input
2500+
2501+
# @result is [⅖ ⅗ ⅜ ⅘ ⅚ ⅝ ⅞]
2502+
2503+
Now we use the feed operator (left-to-right) with parentheses, read top-to-bottom
24992504
2500-
# Feed (left-to-right) with parentheses, read top-to-bottom
25012505
my @result = (
2502-
<people of earth> # (1) Start with the input
2503-
==> map({ .tc }) # (2) Capitalize the words
2504-
==> grep /<[PE]>/ # (3) Look for P or E
2505-
==> sort # (4) Sort, result is <Earth People>
2506+
<TWO THREE FOUR FIVE SEVEN> »~» " " X~ <FIFTHS SIXTHS EIGHTHS> # (1) Input
2507+
==> map( {"VULGAR FRACTION " ~ $^þ } ) # (2) Converts to Unicode name
2508+
==> grep({ .uniparse }) # (3) Filters only real names
2509+
==> map( { .uniparse} ); # (4) Converts to unicode
25062510
);
25072511
2508-
# For illustration, method chaining equivalent, read top-to-bottom
2509-
my @result =
2510-
<people of earth> # (1) Start with the input
2511-
.map({ .tc }) # (2) Capitalize the words
2512-
.grep(/<[PE]>/) # (3) Look for P or E
2513-
.sort; # (4) Sort, result is <Earth People>
2512+
For illustration, method chaining equivalent, read top-to-bottom, using the same sequence as above
2513+
2514+
my @result = ( <TWO THREE FOUR FIVE SEVEN> »~» " " X~ <FIFTHS SIXTHS EIGHTHS>)
2515+
.map( {"VULGAR FRACTION " ~ $^þ } )
2516+
.grep({ .uniparse })
2517+
.map({ .uniparse });
25142518
2515-
# To assign without the need of parentheses use another feed operator
2519+
Although in this particular case the result is the same, the feed operator C«==>» more clearly
2520+
shows intent with arrow pointing the data flow. To assign without
2521+
the need of parentheses use another feed operator
2522+
25162523
my @result;
25172524
<people of earth>
25182525
==> map({ .tc })
25192526
==> grep /<[PE]>/
25202527
==> sort()
25212528
==> @result;
25222529
2523-
# It can be useful to capture a partial result, however, unlike
2524-
# the leftward feed operator, it does require parentheses or a semicolon
2530+
It can be useful to capture a partial result, however, unlike the
2531+
leftward feed operator, it does require parentheses or a semicolon
2532+
25252533
my @result;
25262534
<people of earth>
25272535
==> map({ .tc })

0 commit comments

Comments
 (0)