取数组最长的字符串

# 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: { $^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

你可能感兴趣的:(取数组最长的字符串)