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: 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: 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