【问题标题】:Perl 6 to show index while looping through a listPerl 6在遍历列表时显示索引
【发布时间】:2017-10-11 14:46:05
【问题描述】:

循环遍历一个列表(或数组)时,有没有办法知道循环内当前元素的索引?

当然,问题可以通过循环索引来解决:

my @aa = 8 .. 12;
say "$_\t@aa[$_]" for 0 ..^ @aa.elems;

但也许像下面这样是可能的(我用.CURRENT_INDEX我正在寻找的方法标记)?

my @aa = 8 .. 12;
say $_.CURRENT_INDEX\t$_ for @aa;

【问题讨论】:

  • 试试这个:for @aa.kv -> $i, $_ { say "$i: $_" }

标签: raku


【解决方案1】:

要在列表中获取循环当前元素的循环索引,可以使用列表的.kv 方法。它返回一个交错的索引和值序列:

my @aa = 8 .. 12;
for @aa.kv -> $i, $_ { say "$i: $_" }

输出:

0: 8
1: 9
2: 10
3: 11
4: 12

【讨论】:

  • 谢谢,更优雅!但它仍然是通过 $i 迭代的,它是列表的索引......
【解决方案2】:

TLDR:使用.kv 或.pairs。


这就是幕后真正发生的事情:

my @aa = 8 .. 12;

my \iterator = @aa.iterator;

while ($_ := iterator.pull-one) !=:= IterationEnd {
  say $_
}

在这种情况下,iterator 中的值是一个匿名类,它扮演Iterator 角色。

Iterator 可能有也可能没有任何方法知道它产生了多少值。例如,.roll(*) 的 Iterator 不需要知道到目前为止它产生了多少值,所以它不需要。


Iterator 可以实现返回其当前索引的方法。

my @aa = 8 .. 12;

my \iterator = class :: does Iterator {
  has $.index = 0;     # declares it as public (creates a method)
  has @.values;

  method pull-one () {
    return IterationEnd unless @!values;

    ++$!index;         # this is not needed in most uses of an Iterator
    shift @!values;
  }
}.new( values => @aa );

say "{iterator.index}\t$_" for Seq.new: iterator;
1   8
2   9
3   10
4   11
5   12

您也可以在更高级别的构造中执行此操作;

my @aa = 8 .. 12;

my $index = 0;
my $seq := gather for @aa { ++$index; take $_ };

say "$index\t$_" for $seq;

要让$_.CURRENT-INDEX 工作,需要包装结果。

class Iterator-Indexer does Iterator {
  has Iterator $.iterator is required;
  has $!index = 0;

  method pull-one () {
    my \current-value = $!iterator.pull-one;

    # make sure it ends properly
    return IterationEnd if current-value =:= IterationEnd;

    # element wrapper class
    class :: {
      has $.CURRENT-INDEX;
      has $.value;

      # should have a lot more coercion methods to work properly
      method Str () { $!value }

    }.new( CURRENT-INDEX => $!index++, value => current-value )
  }
}

multi sub with-index ( Iterator \iter ){
  Seq.new: Iterator-Indexer.new: iterator => iter;
}
multi sub with-index ( Iterable \iter ){
  Seq.new: Iterator-Indexer.new: iterator => iter.iterator;
}


my @aa = 8 .. 12;
say "$_.CURRENT-INDEX()\t$_" for with-index @aa.iterator;
# note that $_ is an instance of the anonymous wrapper class

再次使用更高级别的构造:

my @aa = 8 .. 12;

my \sequence := @aa.kv.map: -> $index, $_ {
  # note that this doesn't close over the current value in $index
  $_ but role { method CURRENT-INDEX () { $index }}
}

say "$_.CURRENT-INDEX()\t$_" for sequence;

如果你想要这样的东西,我认为你应该只使用.pairs。 (或使用.kv,但这基本上需要使用带有两个参数的for的块形式)

my @aa = 8 .. 12;
say "$_.key()\t$_.value()" for @aa.pairs;

【讨论】:

  • 布拉德吉尔伯特,非常感谢您的解释!是的,.pairs 对我来说是最好的解决方案。
【解决方案3】:

这是另一种方式,使用您自己的索引变量:

my @aa = 8..12;
say $++, ": $_" for @aa;

输出:

0: 8
1: 9
2: 10
3: 11
4: 12

【讨论】:

  • 但是做一个计数器基本上和循环遍历索引是一样的。
  • 是的!你知道,TMTOWTDI。
猜你喜欢
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 2015-03-16
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多