取数组最长的字符串
2017-06-11 本文已影响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 操作比较慢。