在 Perl 6 中,如何复制 Perl 的 List::Util::all 的行为?

In Perl 6, how can I replicate the behavior of Perl's List::Util::all?

我正在尝试使用 Junction to replicate behavior I am used to in Perl from List::Util::all

我在以下语句中使用 all 联结:

# does not work
return not know(@possible-dates) and not know tell day all(@possible-dates);

不知道这些函数的作用,我假设这个语句等同于以下内容:

# works
my Bool $r = not know @possible-dates;
for @possible-dates -> $date {
  $r = $r && not know tell day $date;
}

return $r;

for 循环版本 returns 正确的结果,连接版本不正确,我试图理解为什么。

下面的完整代码说明了所有函数的作用:

my @dates = <May 15>, <May 16>, <May 19>, <June 17>, <June 18>, 
  <July 14>, <July 16>, <August 14>, <August 15>, <August 17>;

sub day (@date) { @date[1] }
sub month (@date) { @date[0] }

sub tell($data) {
  @dates.grep({ day($_) eq $data or month($_) eq $data });
}

sub know(@possible-dates) { @possible-dates.elems == 1 }

sub statement-one(@date) {
  my @possible-dates = tell month @date;

  # why is this not the same as below?
  return not know(@possible-dates) 
    and not know tell day all(@possible-dates);

  # my Bool $r = not know @possible-dates;
  # for @possible-dates -> $date {
  #   $r = $r && not know tell day $date;
  # }
  #
  # return $r;
}

sub statement-two(@date) {
  my @possible-dates = tell day @date;

  not know(@possible-dates) 
    and know @possible-dates.grep(&statement-one);
}

sub statement-three(@date) {
  my @possible-dates = tell month @date;

  know @possible-dates.grep(&statement-two);
}

sub cheryls-birthday() {
  @dates.grep({ 
    statement-one($_) 
      and statement-two($_) 
      and statement-three($_) 
  });
}

say cheryls-birthday();

我想最简单的答案是做一个

zef install List::Util

然后放一个:

use List::Util 'any';

在你的代码中。 Perl 6 的 anyanyhttp://modules.raku.org/dist/List::Util:cpan:ELIZABETH 提供的 Perl 5 语义之间存在一些细微差别。

在 Perl 6 中,any returns 一个 Junction 对象。在 Perl 5 中,any 是一个函数,您可以在列表上调用一个要执行的块。