是List :: MoreUtils :: Nick越野车吗? [英] Is List::MoreUtils::none buggy?

查看:99
本文介绍了是List :: MoreUtils :: Nick越野车吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为List::MoreUtils中的none子例程不能按所述方式运行.根据文档,

I think none subroutine from List::MoreUtils does not act as described. According to documentation,

无块列表逻辑上取反.如果返回真值 LIST中没有项目符合通过BLOCK,或LIST给出的条件 为空.依次为LIST中的每个项目设置$ _

none BLOCK LIST Logically the negation of any. Returns a true value if no item in LIST meets the criterion given through BLOCK, or if LIST is empty. Sets $_ for each item in LIST in turn

现在,尝试:

use strict;
use warnings;
use 5.012;
use List::MoreUtils qw(none);

my @arr = ( 1, 2, 3 );
if ( none { $_ == 5 } @arr ) {
    say "none of the elements in arr equals 5";
}
else {
    say "some element in arr equals 5";
}

可以,但是将@arr替换为空的(my @arr = ();或简单地my @arr;),则会得到错误的答案.

works OK, but replace @arr with an empty one (my @arr = (); or simply my @arr;) and you get a wrong answer.

这是怎么回事?

更新:我有List :: MoreUtils版本0.22.更新到最新,似乎还可以.虽然很奇怪!

推荐答案

该文档符合v 0.33纯Perl

The documentation is in line with the v 0.33 pure Perl implementation. The reason why it failed was because the implementation changed between versions 0.22 and 0.33.

在v 0.33中,如果@array为空,则不会执行for循环,因此将返回YES.

In v 0.33, if @array is empty, the for loop will not execute, so YES will be returned.

以下是两个版本的并排:

Here are the two versions side-by-side:

# v 0.33                      |  # v 0.22
------------------------------+----------------------------------------
sub none (&@) {               |  sub none (&@) {
    my $f = shift;            |      my $f = shift;
    foreach ( @_ ) {          |      return if ! @_;          # root cause
        return NO if $f->();  |      for (@_) {
    }                         |          return 0 if $f->();
    return YES;               |      }
}                             |      return 1;
                              |  }


MetaCPAN 还提供了 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆