Skip to content

Commit 763904e

Browse files
committed
improvements from #perl6; added working examples
1 parent ee1a853 commit 763904e

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

misc/perl6advent-2010/articles/feed.pod

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@ or written code similar to this:
88
In this construction, the data flows from the C<@original> array which
99
feeds into the C<grep>, and that, in turn, feeds into the C<map>, and
1010
then into the C<sort>, and then finally, the result is assigned to the
11-
C<@new> array. Because they each take a list as their final
12-
parameter, simply by juxtposition, the data feeds from one operation
11+
C<@new> array. Because they each take a list as their final parameter,
12+
Simply by juxtposition, the data feeds leftward from one operation
1313
to the next.
1414

1515
Perl 6, on the other hand, makes this idea of data flowing from one
1616
operation to another explicit by introducing the I<feed operator>. The
17-
above Perl 5 code would be written like this in Perl 6:
17+
above Perl 5 code could be written like this in Perl 6:
1818

1919
my @new <== sort { ... } <== map { ... } <== grep { ... } <== @original;
2020

21-
So, what do we gain from this? Normally, when reading code, you read
22-
from left to right. In the original Perl 5 code you would read from left
23-
to right until you realize that you're dealing with constructions where
24-
the direction of flow is right to left, then you jump to the end and
25-
follow the processing in a right-to-left manner. In Perl 6 there is now
26-
a prominent syntactic marker that clues you in to the leftward flowing
27-
nature of the data.
21+
Note that tmtowtdi is alive and well in Perl 6. You could have also
22+
written much the same as in Perl 5:
23+
24+
my @new = sort { ... }, map { ... }, grep { ... }, @original;
25+
26+
The only difference would be the addition of commas.
27+
28+
So, what do we gain from these feed operators? Normally, when reading
29+
code, you read from left to right. In the original Perl 5 code you
30+
would read from left to right until you realize that you're dealing with
31+
constructions where the direction of flow is right to left, then you
32+
jump to the end and follow the processing in a right-to-left manner. In
33+
Perl 6 there is now a prominent syntactic marker that clues you in to
34+
the leftward flowing nature of the data.
2835

2936
Still, the right-to-left nature of this code is somewhat troublesome.
3037
It may not seem so onerous if all of the code fits on one line as
@@ -33,8 +40,20 @@ C<sort> were little longer. Finding the end of the statement could be a
3340
bit annoying.
3441

3542
Luckily, Perl 6 has another feed operator that allows you to write the
36-
same statement in a left-to-right fashion:
43+
same code in a left-to-right fashion:
3744

3845
@original ==> grep { ... } ==> map { ... } ==> sort { ... } ==> my @new;
3946

47+
This works exactly the same as before only the direction of flow has been changed.
48+
49+
50+
Here are a couple of examples of real, working Perl 6 code using the
51+
feed operators:
52+
53+
my @random-nums = (1..100).pick(*);
54+
my @odds-squared <== sort <== map { $_ ** 2 } <== grep { $_ % 2 } <== @random-nums;
55+
say @odds-squared.join(" ");
4056

57+
my @rakudo-people = <scott patrick carl moritz jonathan jerry stephen>;
58+
@rakudo-people ==> grep { /at/ } ==> map { .ucfirst } ==> my @who-its-at;
59+
say @who-its-at.join(" ");

0 commit comments

Comments
 (0)