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

标签 raku perl6-junction

我正在尝试使用 Junction List::Util::all 复制我在 Perl 中习惯的行为.

我正在使用 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 循环版本返回正确的结果,结点版本没有,我试图理解为什么。

下面的完整代码告知所有函数的作用:
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 的 any 之间有一些细微的差别。和 any使用由 http://modules.raku.org/dist/List::Util:cpan:ELIZABETH 提供的 Perl 5 语义.

在 Perl 6 中,any返回 Junction目的。在 Perl 5 any是一个函数,你可以在一个列表上调用一个带有要执行的块的函数。

关于raku - 在 Perl 6 中,如何复制 Perl 的 List::Util::all 的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50116933/

相关文章:

regex - raku:最长的比赛不做最长的比赛,但在第一场比赛后退出

raku - 如何在 Perl 6 中卸载模块?

raku - 为什么创建 GTK 应用程序会导致依赖错误?

raku - 为什么+和〜对Perl 6连接有不同的影响?

raku - 交换边的双连接点参数

raku - 使用模块加载一组相关函数

raku - 如何在 Perl 6 中制作哈希数组?