Re: iteration (was Re: Angle quotes and pointy brackets)

2004-12-06 Thread David Green
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] (Matt Diephouse) wrote:
On Sat, 04 Dec 2004 08:59:24 -0700, David Green [EMAIL PROTECTED] wrote:

Cdoes Iterate signifies a role named Iterate. Roles are sort of a
mix of interfaces and mixins (as I understand it -- I'm still waiting
for E12). So saying a class fulfills a role just means that it
provides certain methods. In this case, I was saying class with the
Iterate role would provide a C.next method.

I thought of that at first, but I don't want to have to call my 
iterating method next any more than I want to *have* to call my 
constructor new.  But there is a difference in that new is called 
by some user who is supposed to have read the documentation, whereas 
next needs to get implicitly called by for.  

So maybe it really should be a Role.  (One can always provide methods 
with better names that simply call the real .next, .prev, .final, 
etc. for increased user-friendliness.)

eof := final;# is that how to create an alias for a sub/method?

 We've got while for looping, .next for iterating,
  and for for doing both in one convenient little shortcut.

But for needs to know if it has an iterator or a list. You don't want
it iterating over things you didn't want it iterating. In this case, I
was suggesting making an, though I suppose something like
C$sth.execute could just return one.

Well, I was looking at lists as being kinds of iterators. If you want 
to for over an iterator without actually iterating it, I guess 
you'd have to make a reference to it or put it inside a list (so the 
list would be iterated instead).


  - David iterate: to go around and around, like my head Green


Re: iteration (was Re: Angle quotes and pointy brackets)

2004-12-04 Thread David Green
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] (Matt Diephouse) wrote:
What I mean is that Perl takes an array and makes an iterator out of it.
Sure, you probably don't think about it like that, but the behavior is
the same (who says arrays need to iterate starting at element zero?).

I probably didn't, but over the last couple of days I've been thinking 
about it like that more and more.

The odd thing is that here we are designing Perl 6, and we're trying
to take an iterator and make it into an array so that we can turn it
back into an iterator again. It seems like we should just use it as an
iterator::
for $iterator - $elem { ... }

Yes!

Supposing
class Filehandle does Iterate; # Iterate or Iterator?
we have an easy way to create new iterators. I'm not sure how useful
they would be in Perl 6

Maybe the class doesn't do it, but one of its methods does?  Then you 
can call it whatever makes sense.

 class Filehandle
 { method next is iterator {...} }

 class Monarch
 { method succeed is iterator {} }

 class Blockbuster
 { method sequel is iterator { $.title++; return $self; } }

(how do iterators compare to lazy lists?)

Aren't lazy lists a funny kind of iterator?  Ones that memoise their 
results.  And supply an indexing method [].

Which be even cuter like this (I think):
for iter($sth.execute) - $results { ... }
where iter creates an Iterator object that just knows to call C.next
on its argument.

That still seems too cumbersome to me.  Isn't it for that knows to 
call .next (or .sequel, whatever)?  I'm thinking that that is the 
point of for $foo, which should be approximately the same as while 
$foo.next.  We've got while for looping, .next for iterating, 
and for for doing both in one convenient little shortcut.

So lists and arrays would be iterators, although they may not flaunt it 
in public.  But you could always explicitly call their .next method if 
you wanted to.  For example,

 for @lines
 {
  if s/\\$// # ends with a backslash = continued on next line
  {
   $_ ~= @lines.next;
   redo;
  }

  # now process our joined line ...
 }

Of course, that's just the example for redo from the Camel, except 
using an array instead of .  A P5 array wouldn't have worked, because 
there's no way to get the next iteration of an array in the way that 
you can use a scalar  to read the next line of the file. (Though 
there ought to be a better way of referring to the object of the for 
-- I had to refer to it by name here, but I couldn't do that if it were 
a list; and $_ is already taken.  @_ strikes me as reasonable (for a 
not necessarily very large value of reasonable).)

I'm not sure how much extra syntax is needed.  Something that's 
expected to iterate (like a filehandle) should just iterate naturally 
when used in scalar context, or list context, or both.  (But a 
filehandle might stringify to the filename in string context, and 
return the filehandle object itself when being passed to a function 
looking for a filehandle.)

Something that isn't typically expected to iterate (like an array) 
could use its .next method, which is a tad wordy, but that's good 
because that makes it clear and obvious that we are explicitly 
iterating.

Presumably you could slurp up all the iterations at once using * or 
** to flatten them.  That still doesn't get us the magical  because 
it's really a double iteration (over the filenames in @ARGS and then 
over the contents of each file).  In fact, that's just a specific case 
of wanting to loop through several iterators -- Cfor $iter1, $iter2, 
$iter3 {...} only loops through the *list* of iterators, not through 
each object itself.

So maybe we do need Larry's new [EMAIL PROTECTED] to get that kind of 
double-iteration (without having to nest for loops, ugh!).  Hm. 
Unless the flattening operator will take care of that.  Cfor 
*$iter1, *$iter2, *$iter3 {...} would do it, but I'm not sure about 
Cfor [EMAIL PROTECTED] {...}.  (It would definitely do *something*, of that 
I'm fairly confident!)

But I'm starting to think I may have just been thinking the original 
problem all along, only inside-out


Hoping I haven't removed all doubt of my foolishness,

I'm hoping this reply reassures you.


  - David at risk of removing all doubts of mine Green


Re: iteration (was Re: Angle quotes and pointy brackets)

2004-12-04 Thread Matt Diephouse
On Sat, 04 Dec 2004 08:59:24 -0700, David Green [EMAIL PROTECTED] wrote:
 In article [EMAIL PROTECTED],
  [EMAIL PROTECTED] (Matt Diephouse) wrote:
 Supposing
 class Filehandle does Iterate; # Iterate or Iterator?
 we have an easy way to create new iterators. I'm not sure how useful
 they would be in Perl 6
 
 Maybe the class doesn't do it, but one of its methods does?  Then you
 can call it whatever makes sense.

Cdoes Iterate signifies a role named Iterate. Roles are sort of a
mix of interfaces and mixins (as I understand it -- I'm still waiting
for E12). So saying a class fulfills a role just means that it
provides certain methods. In this case, I was saying class with the
Iterate role would provide a C.next method.

 Which be even cuter like this (I think):
 for iter($sth.execute) - $results { ... }
 where iter creates an Iterator object that just knows to call C.next
 on its argument.
 
 That still seems too cumbersome to me.  Isn't it for that knows to
 call .next (or .sequel, whatever)?  I'm thinking that that is the
 point of for $foo, which should be approximately the same as while
 $foo.next.  We've got while for looping, .next for iterating,
 and for for doing both in one convenient little shortcut.

But for needs to know if it has an iterator or a list. You don't want
it iterating over things you didn't want it iterating. In this case, I
was suggesting making an, though I suppose something like
C$sth.execute could just return one.

 Hoping I haven't removed all doubt of my foolishness,
 
 I'm hoping this reply reassures you.

Thanks.

-- 
matt diephouse
http://matt.diephouse.com


Re: iteration (was Re: Angle quotes and pointy brackets)

2004-12-04 Thread Brent 'Dax' Royal-Gordon
David Green [EMAIL PROTECTED] wrote:
 Aren't lazy lists a funny kind of iterator?  Ones that memoise their
 results.  And supply an indexing method [].

As I mentioned the other day, I fail to see any material difference
between an iterator and a lazy list, except that a few operations are
allowed on a lazy list that aren't on an iterator.  (And all of those
could be emulated, albeit inefficiently, with one; even with a pipe,
if the user does $pipe[1024], there's no technical reason you can't
store the first thousand-odd lines and return the one they asked for.)

Also note that there's no difference between iterating over a lazy
copy of an array, and iterating over a lazy copy of a lazy copy of an
array, except for the amount of indirection; thus, there would be no
need for for() to distinguish between Cfor $array and Cfor $iter
(though both of those forms might need a splat).

-- 
Brent 'Dax' Royal-Gordon [EMAIL PROTECTED]
Perl and Parrot hacker

I might be an idiot, but not a stupid one.
--c.l.p.misc (name omitted to protect the foolish)