Re: Coroutines, continuations, and iterators -- oh, my! (Was: Re: Continuations elified)

2002-11-21 Thread fearcadi
Damian Conway writes:
  
  There's no second iterator. Just Cfor walking through an array.
  

( questions in the form of answers :-) 

so : 
* for impose array context for first argument and doesnt care about
  nature of the array which it was given eventually as an argument .
  no multiple streams -- use parallel and friends. 
  
* parallel and friends return lazy array ( for performance
  considerations  ) _o_r_ for *notice* the parallel and ??? optimize
  it away / dont know

  for parallel(@a,@b) - ($x,$y) { ... } 

* every object with next method can function as iterator ???
  _o_r_ we always have to inherit from iterator class . 
  what about reset/rewind  ???

* $fh = open myfile ; 
  for $fh { ... while $fh { ... } ...  }

  both $fh *ultimately* use *the same* method call $fh.next , but
  second $fh does it explicitely , while the first -- from under the
  cloth of lazy array returned by $fh.each and drived by for .

* what does it mean that for walks the array ( keeping in mind that
  that array may be usual or lazy and for have to not to care  )

   
   What's the difference between a lazy array and an iterator? Is there
   caching?
  
  Yes. A lazy array is a wrapper-plus-cache around an iterator.
  

$fh = open file ; 
@a := $fh ; 
print @a[3] # 4 calls to $fh.next 
print @a[0] # no calls to $fh.next 

is that the meaning of ...-plus-cache 


  
   Some of questions about iterators and stuff:
   
   1- Are iterators now considered a fundamental type? 
  
  Probably, since they're fundamental to I/O and Cfor loops.
  
  

so every class can define its own next  method or inherit from
Iterator to be used as an iterator _o_r_ it ( class ) *always* have to 
inherit from Iterator --  to be used as iterator  ??? 
Naively , it seems that this is similar to booleans in perl -- no need to
inherit from special class to behave as boolean. 

  
   2a- Is there an CIterator.toArray() or C.toList() method?
  
  Iterator::each.
  
  
   2a1- The notion that Iterator.each() returns a lazy array seems a
   little wierd. Isn't a lazy array just an iterator?
  
  No. It's an array that populates itself on-demand using an iterator.
  

what is the difference between the arrays @a, @b , ... here 


$a = Iterator.new( ... )  
@a = $a.each ; 
@b := $a.each ; 
@c := $a ; 
@d is lazy = ( 1, 2, 3 ) ;
@f is lazy = $a.each ;


  Iterator: an object that returns successive values from some source
 (such as an array, a filehandle, or a coroutine)

isnt it *anything* having method next ???
why do I need a special type iterator ? 


thanks , 

arcadi . 




Re: Continuations elified

2002-11-20 Thread Damian Conway
Arcadi wrote:


   while $iter {...}  # Iterate until $iter.each returns false?

  you mean Iterate until $iter.next returns false? 

Oops. Quite so.



what is the difference between the Iterator  and lazy array ?

am I right that it is just interface : lazy array is an iterator
object inside Array interface : 


That's one particular implementation of a lazy array, yes.

Another implementation is an array interface with an subroutine that
maps indices onto values. Perl 6 will undoubtedly need both,
and maybe others as well.

Damian





Re: Coroutines, continuations, and iterators -- oh, my! (Was: Re: Continuations elified)

2002-11-20 Thread Damian Conway
Austin Hastings wrote:


   for each $dance: {
  ^ note colon



1- Why is the colon there? Is this some sub-tile syntactical new-ance
that I missed in a prior message, or a new thing?


It's the way we mark an indirect object in Perl 6.



2- Why is the colon necessary? Isn't the each $dance just a
bassackwards method invocation (as Cclose $fh is to C$fh.close())?


Yes. The colon is needed because the colon-less Perl 5 indirect object
syntax is inherently ambiguous. Adding the colon in Perl 6 fixes the
many nasty, subtle problems that Perl 5's syntax had.



I think this is called avoiding the question. Now you've converted an
Iterator into an iterator masked behind an array, and asked the Cfor
keyword to create apparently a private iterator to traverse it.


There's no second iterator. Just Cfor walking through an array.



What's the value of Cnext(PARAM_LIST)? Is this just a shortcut for
re-initializing the iterator?


No. It uses the original coroutine but rebinds its parameters to the new
arguments passed to Cnext.



How is this going to work when the
iterator has opened files or TCP connections based on the parameter
list?


The original files or connections will be unaffected.



Furthermore, what's the syntax for including arguments to next in a
diamond operator?


I very much doubt there would be one. If you need to pass arguments,
you'd call Cnext explicitly.



What's the difference between a lazy array and an iterator? Is there
caching?


Yes. A lazy array is a wrapper-plus-cache around an iterator.



What about the interrelationships between straight iteration
and iteration interrupted by a reset of the parameter list?


Resetting the parameter list doesn't interrupt iteration.


 Or does

calling $iter.next(PARAM_LIST) create a new iterator or wipe the cache?


No.



How do multiple invocations of each() interact with each other? (E.g.,
consider parsing a file with block comment delimiters: one loop to read
lines, and an inner loop to gobble comments (or append to a delimited
string -- same idea). These two have to update the same file pointer,
or all is lost.)


So pass the file pointer to the original continuation.



Some of questions about iterators and stuff:

1- Are iterators now considered a fundamental type? 

Probably, since they're fundamental to I/O and Cfor loops.



1a- If so, are they iterators or Iterators? (See 2b1, below)


class Iterator {...}



1b- What value would iterators (small-i) have? Is it a meaningful idea?


Depends what you mean by it. ;-)



2- What is the relationship between iterators and arrays/lists?


None. Except that some arrays/lists may be implemented using Iterators.



2a- Is there an CIterator.toArray() or C.toList() method?


Iterator::each.



2a1- The notion that Iterator.each() returns a lazy array seems a
little wierd. Isn't a lazy array just an iterator?


No. It's an array that populates itself on-demand using an iterator.



2b- Is there a CList.iterator() method? Or some other standard way of
iterating lists?


Probably.



2b1- Are these primitive interfaces to iteration, in fact
overridable? That is, can I override some operator-like method and
change the behavior of

while $fh { print; }


Sure. Derive a class from Iterator and change its Cnext method.



2b2- Is that what Ceach does in scalar context -- returns an
iterator?


No. Ceach returns a lazy array, so in a scalar context it returns
a reference to the lazy array.



3- What's the difference among an iterator, a coroutine, and a
continuation?


Iterator: an object that returns successive values from some source
	  (such as an array, a filehandle, or a coroutine)

Coroutine: a subroutine whose state is preserved when it returns
   such that it may be restarted from the point of previous
	   return, rather than from the start of the subroutine

Continuation: a mechanism for capturing the what-happens-next
	  at any point in a program's execution

BTW, there's rather a nice discussion of these three at:
http://mail.python.org/pipermail/python-dev/1999-July/000467.html



3a- Does imposing Damian's iterator-based semantics for coroutines
(and, in fact, imposing his definition of any sub-with-yield ==
coroutine) cause loss of desirable capability? 

No. Not compared to other potential coroutine semantics.


 3b- Is there a corresponding linkage between continuations and some

object, a la coroutine-iterator? 

Continuations can be used to implement virtually any control structure.
In a sense they link to everything.



3c- Is there a tie between use of continuations and use of thread or
IPC functionality? 

Hmmm. I *suppose* a continuation could continue into a different thread.
That might be something worth proscribing. ;-)




3d- Conversely, what happens when continuations, coroutines, or
iterators are used in a threaded environment? Will there need to be
locking?


Yes. Threaded environments *always* require locking at some level.



4- Given the 

Re: Continuations elified

2002-11-19 Thread fearcadi
Damian Conway writes:
  David Wheeler asked:
  
   How will while behave?
  
  Cwhile evaluates its first argument in scalar context, so:
  
  
   while $fh {...}# Iterate until $fh.readline returns EOF?
  
  More or less. Technically: call $fh.next and execute the loop
  body if that method returns true. Whether it still has the
  automatic binding to $_ and the implicit definedness check is yet
  to be decided.
  
  
   while $iter {...}  # Iterate until $iter.each returns false?
  
  Yes.
  

you mean Iterate until $iter.next returns false? 


what is the difference between the Iterator  and lazy array ?

am I right that it is just interface : lazy array is an iterator
object inside Array interface : 

Larry Wall wrote:
 Then there's this approach to auto-iteration:
 
 my @dance := Iterator.new(@squares);
 for @dance {

but then each is very simple method : 

class Iterator {
method each( $self:) {
my @a := $self ;
return @a ; 
}
}

but then probably we dont need two methods -- next and each . 
just like in perl5 each can determine the calling context 

class Iterator {
method each( $self:) {
when want Scalar {
...
}
when want Array {
my @a := $self ;
return @a ; 
}
}
}

and then ... could be *really* the sugar for .each ( or .next if it
will be called so ) . in these examples 


 In a scalar context:
 
   $fh   # Calls $fh.readline (or maybe that's $fh.next???
   $iter # Calls $iter.next
   fibs()  # Returns iterator object
   fibs()# Returns iterator object and calls that
   #object's Cnext method (see note below)
   
 
 In a list context:
 
   $fh   # Calls $fh.each
   $iter # Calls $iter.each
   fibs()  # Returns iterator object
   fibs()# Returns iterator object and calls object's Ceach
 

... *always* force call to .each ( which is context aware ) . 

but again all that is based on the assumtion that lazy array is just
Iterator object in the cloth of array ( variable container ) . so
this is a question . 

arcadi 







Re: Coroutines, continuations, and iterators -- oh, my! (Was: Re: Continuations elified)

2002-11-19 Thread Austin Hastings
 Larry wrote:
 
 So you can do it any of these ways:
 
 for $dance {
 
 for $dance.each {
 
 for each $dance: {
^ note colon

1- Why is the colon there? Is this some sub-tile syntactical new-ance
that I missed in a prior message, or a new thing?

2- Why is the colon necessary? Isn't the each $dance just a
bassackwards method invocation (as Cclose $fh is to C$fh.close())? 

 Then there's this approach to auto-iteration:
 
 my @dance := Iterator.new(@squares);
 for @dance {

I think this is called avoiding the question. Now you've converted an
Iterator into an iterator masked behind an array, and asked the Cfor
keyword to create apparently a private iterator to traverse it. That
seems like twice as much work for the same output.

Also, I have a problem with the notion of the Iterator class being
tasked with creation of iterators -- how do you deal with objects (even
TIEd arrays) that require magic iterators? Better to ask the class to
give you one. (Of course, CIterator.new() could internally ask
@squares to provide an iterator, but again that adds a layer for little
apparent gain. 

 Damian Conway wrote:
 The presence of a Cyield automatically makes a subroutine a
 coroutine:
 
   sub fibs {
   my ($a, $b) = (0, 1);
   loop {
   yield $b;
   ($a, $b) = ($b, $a+$b);
   }
   }
 
 Calling such a coroutine returns an Iterator object with (at least)
 the following methods:
 
 next()   # resumes coroutine body until next Cyield
 
 next(PARAM_LIST) # resumes coroutine body until next Cyield,
  # rebinding params to the args passed to Cnext.
  # PARAM_LIST is the same as the parameter list
  # of the coroutine that created the Iterator

What's the value of Cnext(PARAM_LIST)? Is this just a shortcut for
re-initializing the iterator? How is this going to work when the
iterator has opened files or TCP connections based on the parameter
list?

my $iter = DNS.iterator(.com.);

while $iter {
   $iter.next(.com.au.) 
if not hackable($_);
}

Furthermore, what's the syntax for including arguments to next in a
diamond operator?

while $iter($a, $b) {
  ...
  $a += 2;
}

 each()   # returns a lazy array, each element of which
  # is computed on demand by the appropriate
  # number of resumptions of the coroutine body

What's the difference between a lazy array and an iterator? Is there
caching? What about the interrelationships between straight iteration
and iteration interrupted by a reset of the parameter list? Or does
calling $iter.next(PARAM_LIST) create a new iterator or wipe the cache?
How do multiple invocations of each() interact with each other? (E.g.,
consider parsing a file with block comment delimiters: one loop to read
lines, and an inner loop to gobble comments (or append to a delimited
string -- same idea). These two have to update the same file pointer,
or all is lost.)





Some of questions about iterators and stuff:

1- Are iterators now considered a fundamental type? 

1a- If so, are they iterators or Iterators? (See 2b1, below)

1b- What value would iterators (small-i) have? Is it a meaningful idea?

2- What is the relationship between iterators and arrays/lists?

2a- Is there an CIterator.toArray() or C.toList() method?

2a1- The notion that Iterator.each() returns a lazy array seems a
little wierd. Isn't a lazy array just an iterator? Why else have the
proposed syntax for Iterator.next(PARAM_LIST)? (Admittedly the
PARAM_LIST doesn't have to be a single integer, like an array.) Or is
that what a small-i iterator is?

2b- Is there a CList.iterator() method? Or some other standard way of
iterating lists?

2b1- Are these primitive interfaces to iteration, in fact
overridable? That is, can I override some operator-like method and
change the behavior of

while $fh { print; }

2b2- Is that what Ceach does in scalar context -- returns an
iterator?

my $iter = each qw(apple banana cherry);

my $junk = all qw(apple banana cherry);
my $itr2 = each $junk;  # Whoops! Wrong thread...

3- What's the difference among an iterator, a coroutine, and a
continuation?

3a- Does imposing Damian's iterator-based semantics for coroutines
(and, in fact, imposing his definition of any sub-with-yield ==
coroutine) cause loss of desirable capability? (Asked in ignorance --
the only coroutines I've ever dealt with were written in assembly
language, so I don't really know anything about what they can be used
to do.)

3b- Is there a corresponding linkage between continuations and some
object, a la coroutine-iterator? 

3c- Is there a tie between use of continuations and use of thread or
IPC functionality? Is it a prohibitive tie, one way or the other? That
is, I've been thinking that A coroutine is just a continuation. But
if A continuation implies ..., for example a semaphore or a thread,
or some such, then 

Re: Continuations elified

2002-11-18 Thread Austin Hastings

--- Damian Conway [EMAIL PROTECTED] wrote:
 The semantics of Cfor would simply be that if it is given an
 iterator object (rather than a list or array), then it calls 
 that object's iterator once per loop.

By extension, if it is NOT given an iterator object, will it appear to
create one?

That is, can I say 

for (@squares)
{
  ...
  if $special.instructions eq 'Advance three spaces'
  {
$_.next.next.next;
  }
  ...
}

or some other suchlike thing that will enable me to consistently
perform iterator-like things within a loop, regardless of origin?

(Oh please! Let there be one, and for love of humanity, let it be
called bork. Pleasepleaseplease!!! It's a shorthand form of bind or
kontinue, really it is.  :-) :-) :-))


 but I think that's...err...differently right. 

The verb form is euph ? Or euphemise?

 Otherwise I can't see how one call call an iterator directly in a
 for loop:
 
   for fibs() {...}

Which suggests that fibs is a coroutine, since otherwise its return
value is weird. But 

 while fibs() { ... }

suggests instead behavior rather like while (!eof()), although the
diamond is strange. 

So in general, diamonded-function-call implies coroutine/continuation?

 But I could certainly live with it not having that, in which case
 the preceding example would have to be:
 
   my $iter = fibs();
   for $iter {...}

That's not horrible, but it does even more damage to expression
folding.

 and, if your coroutine itself repeatedly yields a iterator
 then you need:
 
   my $iter = fibses();
   for  $iter  {...}
 
 (Careful with those single angles, Eugene!)

To disagree, vile Aussie! To be looking at perl5's adornmentless
diamond:

perlopentut sez:
POT When you process the ARGV filehandle using ARGV, 
POT Perl actually does an implicit open on each file in @ARGV.
POT Thus a program called like this:

POT$ myprogram file1 file2 file3

POT Can have all its files opened and processed one at a time
POT using a construct no more complex than:

POTwhile () {
POT# do something with $_
POT}

This *ought* to work the same in p6.

Since you can modify @ARGV in a pure string context, what's the real
behavior?

If I say: while () {print;} I'm asking for file-scan behavior.

If I say: for (@ARGV) { print; } I'm asking for array-scan behavior.

If I say: for (@ARGV) { print; } I'm asking for trouble?

Proposal:

@ARGV is a string array, but  is topicized in :: (or whatever the
default execution context is called) to iterate over @_MAGIC_ARGV,
which is := @ARGV but of a different class whose iterate behavior
performs an open $_ in the background, and iterates serially over
each entry in @ARGV once EOF occurs.

my CoolFileType @MY_ARGV := @ARGV;  # Same data, different interface. 

for (@MY_ARGV) {
  print;
}

This is kind of neat, but there needs to be a solid, readable
typecasting mechanism to facilitate the multiple flavors of iteration
-- to convert from one iterator format to another, for example.


  They elegantify stuff.
 
 tsk tsk If you're going to talk Merkin, talk it propericiously:
 
   They elegantificatorize stuff

We Merkuns don't add, we shorten. That's elify, to wit:

Z'at elify the code?
Elify - no.

Reduction of length, combined with conservation of ambiguity. Win win
win.

=Austin


__
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com



Re: Continuations elified

2002-11-18 Thread Damian Conway
Austin Hastings asked:



By extension, if it is NOT given an iterator object, will it appear to
create one?


Yep.



That is, can I say 

for (@squares)
{
  ...
  if $special.instructions eq 'Advance three spaces'
  {
$_.next.next.next;
  }
  ...
}

or some other suchlike thing that will enable me to consistently
perform iterator-like things within a loop, regardless of origin?

If, by C$_.next.next.next; you mean skip the next three elements
of @squares, then no. $_ isn't an alias to the implicit iterator
over @squares; it's an alias for the element of @squares currently
being iterated.

You want (in my formulation):

my $dance = Iterator.new(@squares);
for $dance {
   ...
   if $special.instructions eq 'Advance three spaces' {
  $dance.next.next.next;
   }
   ...
}



(Oh please! Let there be one, and for love of humanity, let it be
called bork. Pleasepleaseplease!!! It's a shorthand form of bind or
kontinue, really it is.  :-) :-) :-))


Brain on raw krack more like it ;-)



So in general, diamonded-function-call implies coroutine/continuation?


That's the problem. I can't see how that works syntactically.





To disagree, vile Aussie! To be looking at perl5's adornmentless
diamond:



If I say: while () {print;} I'm asking for file-scan behavior.


Yes. Special case.



If I say: for (@ARGV) { print; } I'm asking for array-scan behavior.


Yes.



If I say: for (@ARGV) { print; } I'm asking for trouble?


grin Under my proposal, you're saying:

	* Grab next element of @ARGV
	* Iterate that element.

*Unless* the elements of @ARGV in Perl 6 are actually special Iterator-ish,
filehandle-ish objects that happen to also stringify to the command-line
strings. Hm.



Proposal:


Seemed very complex to me.



That's elify, to wit:

Z'at elify the code?
Elify - no.


GROAN

Damian




Re: Continuations elified

2002-11-18 Thread Austin Hastings

--- Damian Conway [EMAIL PROTECTED] wrote:
 Austin Hastings asked:
  That is, can I say 
  
  for (@squares)
  {
...
if $special.instructions eq 'Advance three spaces'
{
  $_.next.next.next;
}
...
  }
  
  or some other suchlike thing that will enable me to consistently
  perform iterator-like things within a loop, regardless of origin?
 
 If, by C$_.next.next.next; you mean skip the next three elements
 of @squares, then no. $_ isn't an alias to the implicit iterator
 over @squares; it's an alias for the element of @squares currently
 being iterated.
 
 You want (in my formulation):
 
  my $dance = Iterator.new(@squares);
  for $dance {
 ...
 if $special.instructions eq 'Advance three spaces' {
$dance.next.next.next;
 }
 ...
  }

How'zat, again? What is the means for extracting the actual VALUE of
$dance? And why is that different for $_-as-iterator?

IOW:

my Iterator $dance = ...;
for $dance {
  print $_; # should this print the current dance, - $_ by default?

  print $dance;
  
  # Should the above be .value() 
  # or .next()
  # or .toString() ?

  print $dance; # Obviously get next value and advance a la p5

}

Also, in your formulation:

  my $dance = Iterator.new(@squares);
  for $dance {

What happens when iterators require magic state info? It seems more
appropriate to define a CLASS.iterator method, which overloads a
simplistic default. (fail in scalar cases, iterative for Array and
Hash)

  (Oh please! Let there be one, and for love of humanity, let it be
  called bork. Pleasepleaseplease!!! It's a shorthand form of bind
 or
  kontinue, really it is.  :-) :-) :-))
 
 Brain on raw krack more like it ;-)

Whatever! As long as I get to say:

for my @Swedish - $chef {
  $chef.bork.bork.bork;
}

Perlmuppets, anyone?

  So in general, diamonded-function-call implies
 coroutine/continuation?
 
 That's the problem. I can't see how that works syntactically.

I was thinking in terms of reading, not parsing -- that is, when a
coder reads diamonded-function-call, the reflex will be 'this is a
continuation' -- valuable clues.


  To disagree, vile Aussie! To be looking at perl5's adornmentless
  diamond:
  
  If I say: while () {print;} I'm asking for file-scan behavior.
 
 Yes. Special case.

But a special case of WHAT?

  If I say: for (@ARGV) { print; } I'm asking for trouble?
 
 grin Under my proposal, you're saying:
 
   * Grab next element of @ARGV
   * Iterate that element.

That's the problem. Why would the second point * Iterate that element
happen? We've already GOT a flattener. If I want recursive iteration I
can say:

for (*@ARGV) { print; }

or maybe 

for (*@ARGV) { print; }

I can't be sure.

 *Unless* the elements of @ARGV in Perl 6 are actually special
 Iterator-ish,
 filehandle-ish objects that happen to also stringify to the
 command-line
 strings. Hm.

I thought of that one first, but discarded it.
  Proposal:
 
 Seemed very complex to me.

It's because I discarded the @ARGV is magic that stringifies option.
In other words, let @ARGV be normal, first and foremost. Then apply
magic to it. 

That way, I can argue for being able to apply the SAME magic to
something else, rather than trying to fight with having to reimplement
the @ARGV magic class.

my @file_names = ( list of strings );
my FileIterator @argv_alike := @file_names;

while (@argv_alike) {
 ...
}

Now the question is, how do I get it into $_/@_/whatever_, so that I
can use  instead of @argv_alike? (I avoid simply slapping it into
@_ because I vaguely recall something about the inevitable demise of
@_-as-arglist etc. But that was before the weekend, when there were
more brain cells.)


 GROAN

Warning: File iterator used in void context. Possible loss of data.

=Austin



__
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com



Re: Continuations elified

2002-11-18 Thread Larry Wall
On Tue, Nov 19, 2002 at 08:53:17AM +1100, Damian Conway wrote:
: my $dance = Iterator.new(@squares);
: for $dance {

Scalar variables have to stay scalar in list context, so $dance cannot
suddenly start behaving like a list.  Something must tell the scalar
to behave like a list, and I don't think I want Cfor to do that.
A Cfor should just take an ordinary list.

So you can do it any of these ways:

for $dance {

for $dance.each {

for each $dance: {
   ^ note colon

Then there's this approach to auto-iteration:

my @dance := Iterator.new(@squares);
for @dance {

Larry



Re: Continuations elified

2002-11-18 Thread Damian Conway
Larry wrote:


So you can do it any of these ways:

for $dance {

for $dance.each {

for each $dance: {
   ^ note colon

Then there's this approach to auto-iteration:

my @dance := Iterator.new(@squares);
for @dance {


Okay, so now I need to make sense of the semantics of ... and
Cfor and coroutines and their combined use.

Is the following correct?

==

The presence of a Cyield automatically makes a subroutine a coroutine:

	sub fibs {
		my ($a, $b) = (0, 1);
		loop {
			yield $b;
			($a, $b) = ($b, $a+$b);
		}
	}

Calling such a coroutine returns an Iterator object with (at least)
the following methods:

	next()			# resumes coroutine body until next Cyield

	next(PARAM_LIST)	# resumes coroutine body until next Cyield,
# rebinding params to the args passed to Cnext.
# PARAM_LIST is the same as the parameter list
# of the coroutine that created the Iterator

	each()			# returns a lazy array, each element of which
# is computed on demand by the appropriate
# number of resumptions of the coroutine body


In a scalar context:

	$fh		# Calls $fh.readline (or maybe that's $fh.next???
	$iter		# Calls $iter.next
	fibs()		# Returns iterator object
	fibs()	# Returns iterator object and calls that
			#object's Cnext method (see note below)
	

In a list context:

	$fh		# Calls $fh.each
	$iter		# Calls $iter.each
	fibs()		# Returns iterator object
	fibs()	# Returns iterator object and calls object's Ceach


So then:

	for $fh {...}# Build and then iterate a lazy array (the elements
			   # of which call back to the filehandle's input
			   # retrieval coroutine)

	for $iter {...}  # Build and then iterate a lazy array (the elements
			   # of which call back to the iterator's coroutine)

	for fibs() {...}   # Loop once, setting $_ to the iterator object
			   # that was returned by Cfibs

	for fibs() {...} # Build and then iterate a lazy array (the elements
			   # of which call back to the coroutine of the
			   # iterator returned by Cfibs


==

Note: this all hangs together *very* nicely, except when someone writes:

	loop {
		my $nextfib = fibs();
		...
	}

In which case $nextfib is perennially 1, since every call to Cfibs
returns a new Iterator object.

The solution is very simple, of course:

		my $nextfib = my $iter//=fibs();

but we might want to contemplate issuing a warning when someone calls
an argumentless coroutine within a scalar context 

Damian




Re: Continuations elified

2002-11-18 Thread David Wheeler
On Monday, November 18, 2002, at 06:51  PM, Damian Conway wrote:


	for $fh {...}# Build and then iterate a lazy array (the elements
			   # of which call back to the filehandle's input
			   # retrieval coroutine)

	for $iter {...}  # Build and then iterate a lazy array (the elements
			   # of which call back to the iterator's coroutine)

	for fibs() {...}   # Loop once, setting $_ to the iterator object
			   # that was returned by Cfibs

	for fibs() {...} # Build and then iterate a lazy array (the elements
			   # of which call back to the coroutine of the
			   # iterator returned by Cfibs


How will while behave?

	while $fh {...}# Iterate until $fh.readline returns EOF?

	while $iter {...}  # Iterate until $iter.each returns false?

	while fibs() {...}   # Infinite loop -- fibs() returns an
 # iterator every time?

	while fibs() {...} # I'm afraid to ask!

Best,

David

--
David Wheeler AIM: dwTheory
[EMAIL PROTECTED] ICQ: 15726394
http://david.wheeler.net/  Yahoo!: dew7e
   Jabber: [EMAIL PROTECTED]




Re: Continuations elified

2002-11-18 Thread Luke Palmer
 Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
 X-Sent: 19 Nov 2002 02:51:54 GMT
 Date: Tue, 19 Nov 2002 13:51:56 +1100
 From: Damian Conway [EMAIL PROTECTED]
 X-Accept-Language: en, en-us
 Cc: [EMAIL PROTECTED] [EMAIL PROTECTED]
 X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/
 
 Larry wrote:
 
  So you can do it any of these ways:
  
  for $dance {
  
  for $dance.each {
  
  for each $dance: {
 ^ note colon
  
  Then there's this approach to auto-iteration:
  
  my @dance := Iterator.new(@squares);
  for @dance {
 
 Okay, so now I need to make sense of the semantics of ... and
 Cfor and coroutines and their combined use.
 
 Is the following correct?
 
 ==
[snip] 
 ==


I like this Imuch better than what you explained before.  Most of my
problems with two iterators to the same thing in the same scope are
gone, as well as the confusions I had about Cfor.

Luke



Re: Continuations elified

2002-11-18 Thread Damian Conway
David Wheeler asked:


How will while behave?


Cwhile evaluates its first argument in scalar context, so:



while $fh {...}# Iterate until $fh.readline returns EOF?


More or less. Technically: call $fh.next and execute the loop body if that method
returns true. Whether it still has the automatic binding to $_ and the implicit
definedness check is yet to be decided.



while $iter {...}  # Iterate until $iter.each returns false?


Yes.



while fibs() {...}   # Infinite loop -- fibs() returns an
 # iterator every time?


I suspect so.



while fibs() {...} # I'm afraid to ask!


Usually an infinite loop. Cfibs() returns a new iterator every time,
which ... then calls Cnext on.

Damian




Re: Continuations elified

2002-11-18 Thread David Wheeler
On Monday, November 18, 2002, at 08:05  PM, Damian Conway wrote:


while $fh {...}# Iterate until $fh.readline returns EOF?


More or less. Technically: call $fh.next and execute the loop body 
if that method
returns true. Whether it still has the automatic binding to $_ and the 
implicit
definedness check is yet to be decided.

That's a scalar context? I assumed it was list context from your 
previous post:

In a list context:

	$fh		# Calls $fh.each


At any rate, I hope that it's bound to $_ -- nice conversion from Perl 
5's behavior, that.

David

--
David Wheeler AIM: dwTheory
[EMAIL PROTECTED] ICQ: 15726394
http://david.wheeler.net/  Yahoo!: dew7e
   Jabber: [EMAIL PROTECTED]



Re: Continuations elified

2002-11-18 Thread Damian Conway
David Wheeler asked:


while $fh {...}# Iterate until $fh.readline returns EOF?



That's a scalar context? 

Sure. Cwhile always evaluates its condition in a scalar context.

Damian




Re: Continuations elified

2002-11-18 Thread David Wheeler
On Monday, November 18, 2002, at 08:17  PM, Damian Conway wrote:


Sure. Cwhile always evaluates its condition in a scalar context.


Oh, duh. Thanks.

David

--
David Wheeler AIM: dwTheory
[EMAIL PROTECTED] ICQ: 15726394
http://david.wheeler.net/  Yahoo!: dew7e
   Jabber: [EMAIL PROTECTED]