Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
FOP utilities
#perl 5.26.1 #some utilities function to bring some more FOP flavour. sub foldl{my $f=shift; my $r=shift; $r=&$f($r,shift) while scalar(@_); $r} sub foldr{my $f=shift; my $r=shift; $r=&$f(pop,$r) while scalar(@_); $r} sub curry{my $f=shift; sub{return &$f unless scalar(@_); my @a=@_; curry(sub {$f->(@a,@_)})}} sub uncurry{my $f=shift; sub{$f->(@_)()}} #e.g. $f=sub{foldl sub{$_[0]+$_[1]}, 0,@_}; # curry($f)->(1)(2)(3,4,5)(6)() == $f->(1,2,3,4,5,6) ==21 sub curry_once{my $f=shift; sub{my @a=@_; sub{$f->(@a,@_)}}} sub comp{my $f=shift; (ref $f eq 'CODE')?&$f:$f} sub zipWith($++){my $f=shift; my @a=@{;shift}; my @b=@{;shift}; map {$f->($a[$_],$b[$_])} (0.._min($#a,$#b))} sub zip(++){unshift @_,sub{[@_]}; goto &zipWith} sub zipWithMany{my $f=shift; $f=(ref $f eq 'ARRAY')?$$f[0]:curry($f); # MAGIC. if(scalar(@_)==1){return map {$_->()} @{$_[0]}} # TILTOWAIT! my @a=zipWith($f,shift,shift); my $ap=sub{$_[0]->($_[1])}; zipWithMany([$ap],[@a],@_)} sub rec{my $f=curry_once shift; #the Y combinator, folks! sub{my $x=shift; $x->($x)}->( #using it is a BAD idea! sub{my $y=shift; $f->( sub{$y->($y)->(@_)})})} sub Op{my $op=shift; sub{eval"\$_[0]$op\$_[1]"}} #e.g. (Op '+') is the addition function sub _min{$_[0]<$_[1]?$_[0]:$_[1]}
run
|
edit
|
history
|
help
0
Hello
grid
Perl
Neha101736
Hash
Attractive Numbers
Perl logo
Challenge 41 Task 1
101772
Price_supriya