取数组最长的字符串

# 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,792评论 0 33
  • 一笔淡妆,一袭白衣,不过如此。 风月阁在在槐阳城颇有名气,逢人皆言花魁白溪貌似出水芙蓉,却性如淤泥莲花,独占一方之...
    研一笙阅读 1,044评论 0 0
  • 油菜花盛开的季节,黄灿灿的油菜花、蓝天白云和青海湖蔚蓝的湖水连成一片,想象了好多年的情景终于有机会亲眼一睹。 “黄...
    云掩月念阅读 384评论 0 2
  • 这段时间一直都没有怎么有空写东西,工作比较忙,也是因为没有时间看电影,不知道要写点什么,那天又看了一遍《魔幻厨房》...
    爱丫的电影美食和育儿阅读 649评论 0 1
  • 因为半个月前动了个手术,所以一直在家休养,这段日子以来,婆婆又帮忙带小孩又要照顾我,确实是辛苦了(我自己的妈妈也会...
    大脸猫8584阅读 304评论 0 1