When we slice an array with an index that exceeds the boundaries of the array we get as the result the undefined (Any)
When we pass the same slice index as a lazy list then we get as result the existing values of the array/list (and NOT any more than that):
my @a = ^5;
say @a[^10]; # (0 1 2 3 4 (Any) (Any) (Any) (Any) (Any))
say @a[lazy ^10]; # (0 1 2 3 4)
It is clear that lazyness of the slice index affects the result.
Trying to undestand the way things are and as a proof of concept I programmed my simple version of the slice mechanism:
my @a = ^5;
my @s1 = ^10;
my @s2 = lazy ^10;
sub postcircumfix:<-[ ]-> (@container, @index) {
my $iter = @index.iterator;
gather {
loop {
my $item := $iter.pull-one;
if $item =:= IterationEnd {
last;
}
with @container[$item] {
take @container[$item]
} else {
@index.is-lazy ?? { last } !! take @container[$item];
}
}
}
}
say @a-[@s1]-; # (0 1 2 3 4 (Any) (Any) (Any) (Any) (Any))
say @a-[@s2]-; # (0 1 2 3 4)
But I am wondering if my naive algorithm depicts the way that things are computed under the hood !
The source for how things are done under the hood can be found in array_slice.pm6.
Specifically, you can see the following at L73:
So, as you've surmised, it does indeed stop after a list item doesn't exist. This is no doubt becaue many lazy lists are infinite, and iterators don't provide a way to know if they are infinite or not (the generator may be non-determinative).
If you really want to enable such a thing, you could, for instance, write your own slicer that handles lazy lists where an element may not be available, but you have to take care to ensure that things are only eagerly evaluated if you know they're finite:
The output of this is