Are there some features like zip and fold in Perl?

I want to use some function like "zip" , "fold" and "map" in perl. (Like in Haskell.) I found a map, and it works well. Then, zipper and crease? Thank you very much.

+4
source share
4 answers

In my List :: Gen module, I implemented many of these functions (and even Haskell-like lazy ones)

use List::Gen qw(zip reduce); my @list = zip [1 .. 4], ['a' .. 'd']; my $str = reduce {$a . $b} @list; say $str; # 1a2b3c4d 

Or using the glob function to build ranges:

 use List::Gen 'glob'; say <1 .. 4>->zip(<a .. d>)->reduce('$a.$b'); # 1a2b3c4d 

Using ->reduce('.') Or ->reduce(sub {$a . $b}) also works.

Or if you play golf:

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

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

+8
source

The List::Util library has reduce() , which does what fold does.

List::MoreUtils contains the zip() function.

Not built-in, mainly because Perl is not a functional programming language.

+10
source

If you are interested in using functional programming concepts in Perl, I highly recommend that you read the higher order of Perl .

+4
source

Modules that provide functional programming tools:

You may also be interested in a book (now free to download) Higher Perl order .

+2
source

Source: https://habr.com/ru/post/1394422/


All Articles