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

lazy list syntax?

2 views
Skip to first unread message

Flavio S. Glock

unread,
Jul 27, 2005, 7:17:41 PM7/27/05
to perl6-l...@perl.org
How can I create a lazy list from an object?

I have an object representing the sequence "1..Inf".
I tried creating a Coroutine, and then assigning the Coroutine to an
Array, but it only yielded "1":

my @a = $span.lazy; # "1"

The coroutine worked fine in a "while" loop, but it didn't work in a "for" loop.

This is the implementation (in "ext/Span"):

coro lazy ($self: ) {
my $iter = $self.iterator();
loop {
my $n = $iter.next;
return unless defined $n;
yield $n;
}
}

I understand that this is not fully specified yet, but I'd like to
start writing some tests for it.

Thanks!
- Flavio S. Glock

Yuval Kogman

unread,
Jul 28, 2005, 10:17:59 AM7/28/05
to Flavio S. Glock, perl6-l...@perl.org
On Wed, Jul 27, 2005 at 20:17:41 -0300, Flavio S. Glock wrote:
> I have an object representing the sequence "1..Inf".
> I tried creating a Coroutine, and then assigning the Coroutine to an
> Array, but it only yielded "1":
>
> my @a = $span.lazy; # "1"
>
> The coroutine worked fine in a "while" loop, but it didn't work in a "for" loop.

I think unary = is what you want:

my @a = $span.lazy;

for =@a -> $item {
...
}

Ofcourse, my @a = $span.lazy will have to be fixed, but what you
tried should be working.

--
() Yuval Kogman <nothi...@woobling.org> 0xEBD27418 perl hacker &
/\ kung foo master: *shu*rik*en*sh*u*rik*en*s*hur*i*ke*n*: neeyah!!!!

Flavio S. Glock

unread,
Jul 28, 2005, 6:58:16 PM7/28/05
to perl6-l...@perl.org
2005/7/28, Yuval Kogman <nothi...@woobling.org>:

>
> I think unary = is what you want:
>
> my @a = $span.lazy;
>
> for =@a -> $item {
> ...
> }
>
> Ofcourse, my @a = $span.lazy will have to be fixed, but what you
> tried should be working.

Is "for =" only for filehandles? I tried:

pugs> say for =1
*** cannot cast from VInt 1 to Handle (VHandle)

- Flavio S. Glock

Yuval Kogman

unread,
Jul 28, 2005, 7:08:48 PM7/28/05
to Flavio S. Glock, perl6-l...@perl.org

Hmmm... That's odd. I may be out of date, but I thought that unary =
is the complement of unary **.

Aankhen

unread,
Jul 29, 2005, 3:03:15 AM7/29/05
to perl6-l...@perl.org
On 7/29/05, Flavio S. Glock <fgl...@gmail.com> wrote:
> Is "for =" only for filehandles? I tried:

No, it's for anything that supports iteration... `=$foo` ==
`$foo.next()`, if I recall correctly. It's probably not yet
implemented.

Aankhen

Flavio S. Glock

unread,
Jul 29, 2005, 1:37:06 PM7/29/05
to perl6-l...@perl.org
Just wondering - would 'reverse =$foo' call '$foo.previous()' ?

- Flavio

2005/7/29, Aankhen <aan...@gmail.com>:

0 new messages