hm.. consider that:

perl5:
   open $fh, 'file';
   $first_line = <$fh>;
   @remaining = <$fh>;

perl6:
   $fh = open 'file';
   $first_line = $fh();
   @remaining = $fh();

I thought about parallels between arrays and iterators, and realized that they aren't very close to each other:
iterators are much closer to subroutines. And moreover, in perl5 they ARE subroutines. In abstract, arrays are things with plenty of access methods and iterator is the thing with a single action: "one more result, please". and that simplicity IS a power.
so why not just CALL our iterator?


of course, that analogy isn't going to work for "true" functions, which returns the same all the time, for some given set of arguments.

so, of course, arrays and iterators should be interchangeable, but it could be kin to an autoconversion. And we for sure have no need to convert iterator to array so that we should convert it back again later, for C<for> etc. (as some people here already stated)

ok, "structured" part of my brain is over.. now,
random thoughts:

 $fh(:chomp)

for &$fh {...}

for &$fh.assuming(:chomp) {...}

# what about making &foo( :bar ) the same as
#  &foo.assuming( :bar) ? ah, that not gonna work.. but I
# think that feature definitely wants something more short
# than .assuming; maybe even some special syntactic honey.
# because it's  fun(ny|ctional)

class Foo {
  has $.counter;
  # multi postcircumfix:<( )> makes me sick :(
  method bar is default {
     $.counter++;
  }
}

# emulating   10...
$a = new Foo :counter(10);
say $a.bar;
say $a();

# other thingies:

 sub foo { ... };
 @array = @&foo # all the results of foo, lazily.

maybe  &@array and @array()  could make a sense too:

  $fibonacci = {
     state ($a, $b) = (1,1);
     while $b <100 {
       ($a,$b) = ($b, $b+$a);
       yield $a
     }
  }

(ah, that isn't very short and impressive.. I need to study a course of "Design of Short and Impressive Examples", by Damian or Mark-Jason, maybe.)

  for @$fibonacci ... # or...
  for &$fibonacci ... # or...
  for $fibonacci ...

interesting problem: how could we iterate through a list of iterators, one by one, without flattening 'em?

and the very last and most important problem: I have no idea how we could signal an end of sequence. Throw an exception? return undef but something_special?
or maybe even perform some self-destructing action?


{
   undef &whatever_we_have_at_this_moment_standing_for_current_function
      if end_condition
}

Reply via email to