Perl有一些功能,如zip和fold吗? [英] Is there some functions like zip and fold in Perl?

查看:98
本文介绍了Perl有一些功能,如zip和fold吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一些功能,例如zipfoldmap在perl。 (就像在Haskell。)我发现地图,它的效果很好。那么拉链和褶皱呢?非常感谢你。

I want to use some function such as "zip", "fold" and "map" in perl. (Just like in Haskell.) I found map and it works well. Then, is there zip and fold? Thank you very much.

推荐答案

我已经在我的模块中实现了许多这些功能(甚至是Haskell般的懒惰) 列表:: Gen

I've implemented many of those functions (and even Haskell-like lazy ones) in my module List::Gen

use List::Gen qw(zip reduce);

my @list = zip [1 .. 4], ['a' .. 'd'];

my $str = reduce {$a . $b} @list;

say $str; # 1a2b3c4d

或使用 glob 函数构建范围:

use List::Gen 'glob';

say <1 .. 4>->zip(<a .. d>)->reduce('$a.$b'); # 1a2b3c4d

使用 - > reduce('。') - > reduce(sub {$ a。$ b})也可以使用。

或者如果你打高尔夫球:

Or if you are golfing:

say <[.]>->(<1..4>|<a..d>);

或者对于Haskell版本,请参阅列表:: Gen :: Haskell

Or for the Haskell versions, see List::Gen::Haskell

这篇关于Perl有一些功能,如zip和fold吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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