Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[svn:perl6-synopsis] r13543 - doc/trunk/design/syn

5 views
Skip to first unread message

la...@cvs.perl.org

unread,
Jan 29, 2007, 1:39:27 PM1/29/07
to perl6-l...@perl.org
Author: larry
Date: Mon Jan 29 10:39:25 2007
New Revision: 13543

Modified:
doc/trunk/design/syn/S04.pod

Log:
Note that take is intended to work en passant as suggested by gabriele renzi++
Also clarified that gather provides a void context to its victim.


Modified: doc/trunk/design/syn/S04.pod
==============================================================================
--- doc/trunk/design/syn/S04.pod (original)
+++ doc/trunk/design/syn/S04.pod Mon Jan 29 10:39:25 2007
@@ -12,9 +12,9 @@

Maintainer: Larry Wall <la...@wall.org>
Date: 19 Aug 2004
- Last Modified: 25 Jan 2007
+ Last Modified: 29 Jan 2007
Number: 4
- Version: 48
+ Version: 49

This document summarizes Apocalypse 4, which covers the block and
statement syntax of Perl.
@@ -435,13 +435,33 @@
explicit because it's not out front where it can be seen. You can, of
course, use a placeholder parameter if you also use C<return>.)

+=head2 The gather statement
+
A variant of C<do> is C<gather>. Like C<do>, it is followed by a
-statement or block, and executes it once. Unlike C<do>, its return
-value is specified by calling the C<take> function one or more times
-within the dynamic scope of the gather. The returned values are in
-the form of a lazy multislice, with each dimension corresponding to
-one C<take> slice. (A multislice is flattened in most list contexts.).
-A C<gather> is not considered a loop.
+statement or block, and executes it once. Unlike C<do>, it evaluates the
+statement or block in void context; its return
+value is instead specified by calling the C<take> function one or more times
+within the dynamic scope of the gather. The returned values are in the
+form of a lazy multislice, with each slice corresponding to one
+C<take> capture. (A multislice is lazily flattened in normal list context,
+but you may "unflatten" it again with a @@() contextualizer.)
+
+Because C<gather> evaluates its block or statement in void context,
+this typically causes the C<take> statement to be evaluated in void
+context. However, a C<take> statement that is not in void context
+gathers its arguments I<en passant> and also returns them unchanged.
+This makes it easy to keep track of what you last "took":
+
+ my @uniq = gather for @list {
+ state $previous = take $_;
+ next if $_ === $previous;
+ $previous = take $_;
+ }
+
+A C<gather> is not considered a loop, but it is easy to combine with a loop
+as in the example above.
+
+=head2 Other C<do>-like forms

Other similar C<Code>-only forms may also take bare statements,
including C<try>, C<contend>, C<async>, and C<lazy>. These constructs

Gaal Yahas

unread,
Jan 29, 2007, 3:01:08 PM1/29/07
to la...@cvs.develooper.com, perl6-l...@perl.org
On Mon, Jan 29, 2007 at 10:39:27AM -0800, la...@cvs.develooper.com wrote:
> +Because C<gather> evaluates its block or statement in void context,
> +this typically causes the C<take> statement to be evaluated in void
> +context. However, a C<take> statement that is not in void context
> +gathers its arguments I<en passant> and also returns them unchanged.
> +This makes it easy to keep track of what you last "took":
> +
> + my @uniq = gather for @list {
> + state $previous = take $_;
> + next if $_ === $previous;
> + $previous = take $_;
> + }

What does it mean for take to be evaluated in void context?

What are the gathered values here?

take 1, 2; # easy. flattened 1 and then 2, right?
@x = take 1, 2; # same thing?
$x = take 1, 2; # same thing? [1, 2]?

--
Gaal Yahas <ga...@forum2.org>
http://gaal.livejournal.com/

Gaal Yahas

unread,
Jan 29, 2007, 3:08:34 PM1/29/07
to perl6-l...@perl.org
On Mon, Jan 29, 2007 at 10:01:08PM +0200, Gaal Yahas wrote:
> > +Because C<gather> evaluates its block or statement in void context,
> > +this typically causes the C<take> statement to be evaluated in void
> > +context. However, a C<take> statement that is not in void context
> > +gathers its arguments I<en passant> and also returns them unchanged.
> > +This makes it easy to keep track of what you last "took":
> > +
> > + my @uniq = gather for @list {
> > + state $previous = take $_;
> > + next if $_ === $previous;
> > + $previous = take $_;
> > + }
>
> What does it mean for take to be evaluated in void context?
>
> What are the gathered values here?
>
> take 1, 2; # easy. flattened 1 and then 2, right?
> @x = take 1, 2; # same thing?
> $x = take 1, 2; # same thing? [1, 2]?

In fact, $x = take 5; # if this were Perl 5, I might expect
# either 1 or [1] here!

Gaal Yahas

unread,
Jan 29, 2007, 3:11:58 PM1/29/07
to perl6-l...@perl.org

Ugh, sorry, I meant either 1 or [5].

0 new messages