# New Ticket Created by  Jaume Martí 
# Please include the string:  [perl #74008]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=74008 >


This patch implements lazy assignation to Seq and Array objects.


Some things that don't work yet:
* for statements are eager, so a for iterating an infinite set can't be used
on a gather.
* Circular side-effects.
* Either class IO or test io.rakudo need changes, because they don't have
the same semantics when gathers are assigned lazily to Arrays.

The test below can be used to evaluate the patch:

# Ranges

my @range_a=1..*;

is @range_a[0..5].join(','), '1,2,3,4,5,6', 'An infinite range on an
C<Array>';

my @range_b=2,4,6..*;

is @range_b[0..5].join(','), '2,4,6,7,8,9', 'An infinite range on an
C<Array> with initial values';


# Series

my @series_a=2,4,6...*;

is @series_a[0..5].join(','), '2,4,6,8,10,12', 'Infinite series
2,4,6,8,10,12... on an C<Array>';

my @series_b=2,4,8...*;

is @series_b[0..5].join(','), '2,4,8,16,32,64', 'Infinite series
2,4,8,16,32,64... on an C<Array>';

my @series_c=0,1,{ $^a + $^b }...*;

is @series_c[0..5].join(','), '0,1,1,2,3,5', 'All fibonacci numbers on an
C<Array>';


# Gather

my @gather_a = gather { my $n=1; loop { take $n++; } };

is @gather_a[0..5].join(','), '1,2,3,4,5,6', 'Infinite C<gather/take> on an
C<Array>';


# Side-effects

my $i=0;

my @side_1 = gather { loop { take $i++ } };

my @side_2 = @side_1;

my @side_3 = @side_2;

is @side_1[0..5].join(','),'0,1,2,3,4,5','Assigning C<Gather> with
side-effects';

is @side_2[0..5].join(','),'0,1,2,3,4,5',''Assigning C<Gather> with
side-effects';

is @side_3[0..5].join(','),'0,1,2,3,4,5',''Assigning C<Gather> with
side-effects';


# Parcels with multiple potentially infinite sets.

my @pinf_1 = gather { for 2..5 { take $_ } };
my @pinf_2 = 8...*;
my @pinf_3 = 0...*;

my @parcel_1 = (1,@pinf_1,6,7,@pinf_2);
my @parcel_2 = (1,@pinf_3,6,7,@pinf_2);

is @parcel_1[0..9].join(','),'1,2,3,4,5,6,7,8,9,10';

is @parcel_2[0..9].join(','),'1,0,1,2,3,4,5,6,7,8','';

my @map_1=1,2..*;
@map_1.=map({$_*2});

is @map_1[0..5].join(','),'2,4,6,8,10,12','Lazy map';


my @grep_1=1,2..*;
@grep_1.=grep({$_>2});

is @grep_1[0..5].join(','),'3,4,5,6,7,8','Lazy grep';

Attachment: 0001-Implementation-of-lazy-Seq-and-Array.patch
Description: Binary data

Reply via email to