slice - 切片索引的惰性如何影响数组列表的切片? [乐]

标签 slice raku lazy-sequences rakudo

当我们使用超出数组边界的索引对数组进行切片时,我们得到的结果是 undefined (Any)
当我们传递与惰性列表相同的切片索引时,我们将获得数组/列表的现有值(并且仅此而已):

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)
很明显,切片索引的惰性会影响结果。
为了弄明白事情的本质,作为概念证明,我编写了我的切片机制的简单版本:
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)
但我想知道我的幼稚算法是否描述了事物在后台计算的方式!

最佳答案

可以在 array_slice.pm6 中找到如何在引擎盖下完成工作的来源。 .
具体可以在L73看到以下内容:

    if is-pos-lazy {
        # With lazy indices, we truncate at the first one that fails to exists.
        my \rest-seq = Seq.new(pos-iter).flatmap: -> Int() $i {
            nqp::unless(
              $eagerize($i),
              last,
              $i
            )
        };
        my \todo := nqp::create(List::Reifier);
        nqp::bindattr(todo, List::Reifier, '$!reified', eager-indices);
        nqp::bindattr(todo, List::Reifier, '$!current-iter', rest-seq.iterator);
        nqp::bindattr(todo, List::Reifier, '$!reification-target', eager-indices);
        nqp::bindattr(pos-list, List, '$!todo', todo);
    }
    else {
        pos-iter.push-all: target;
    }
因此,正如您所猜测的那样,它确实会在列表项不存在后停止。这无疑是因为许多惰性列表是无限的,并且迭代器没有提供一种方法来知道它们是否是无限的(生成器可能是不确定的)。
如果您真的想启用这样的功能,例如,您可以编写自己的切片器来处理元素可能不可用的惰性列表,但是您必须小心确保只有在您知道它们的情况下才会急切地评估它们'是有限的:
multi sub postcircumfix:<-[ ]-> (@a, @b) {
  lazy gather {
    take @a[$_] for @b;
  }
}

my @a = ^5;
my @b = lazy gather { 
  for ^10 -> $i { 
    # So we can track when elements are evaluated
    say "Generated \@b[$i]"; 
    take $i;
  } 
};

say "Are we lazy? ", @a-[@b]-;
say "Let's get eager: ", @a-[@b]-.eager;
say "Going beyond indices: ", @a-[@b]-[11]
这个的输出是
Are we lazy? (...)
Generated @b[0]
Generated @b[1]
Generated @b[2]
Generated @b[3]
Generated @b[4]
Generated @b[5]
Generated @b[6]
Generated @b[7]
Generated @b[8]
Generated @b[9]
Let's get eager: (0 1 2 3 4 (Any) (Any) (Any) (Any) (Any))
Going beyond indices: Nil

关于slice - 切片索引的惰性如何影响数组列表的切片? [乐],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64121622/

相关文章:

python - Python 扩展切片表示法的正式语法?

python - 这行代码中的[0]是什么意思?

raku - 在 Raku 中一步使用变量并为其分配表达式

clojure - Clojure 有 "unfold"吗?

haskell - mapM 的懒惰版本

javascript - Coffeescript/Javascript 为什么,克隆数组中的对象在传递给方法时似乎丢失了它们的引用?

go - 是 slice2 := slice1 equal to slice2 := slice1[:] in GoLang?

perl - 您如何自定义文本分段以在有向图之间不中断?

raku - 在接收器上下文中抛出的 block 的最后一个元素

c++ - C++17 是否提供生成器或其他内置方式来并行转换非实例化序列?