美文网首页Raku Programming Language
取数组最长的字符串

取数组最长的字符串

作者: 焉知非鱼 | 来源:发表于2017-06-11 20:32 被阅读7次
    # quote words, but with quote protection!
    my @strings = qww/
        "This is a line"
        short
        "A very very long string maybe, certainly the longest"
        other
        not
        this
        a
        one
        or
        even
        1
        /;
    
    # If you give one thing to sort, it does that to both things and
    # compares with camp
    #my ($longest) = @strings.sort( *.chars ).[*-1];
    #say $longest;
    
    #my sub infix:<longest> { $^a.chars > $^b.chars ?? $^a !! $^b }
    #my $longest = [longest] @strings;
    
    #say "Longest is 「$longest」";
    
    #my ($longest) = max :by{$^a.chars <=> $^b.chars}, @strings;
    #say $longest;
    
    my $longest = @strings.max: *.chars ;
    say $longest;
    

    reduction 操作比较慢。

    quick-trick-longest-string-using-the-reduction-operator

    相关文章

      网友评论

        本文标题:取数组最长的字符串

        本文链接:https://www.haomeiwen.com/subject/whqkqxtx.html