我正在尝试使用 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/