More Related Content
Similar to Functional perl (20)
Functional perl
- 18. sort String sorting
sort {$a cmp $b} Also string sorting
sort {$a <=> $b} Sort numerically
sort &comparator Sort by function
named comparator
- 19. sort {
@a = split ///, $a;
@b = split ///, $b;
($a[2] <=> $b[2])
|| ($a[1] <=> $b[1])
|| ($a[0] <=> $b[0])
|| 0;
} @list_of_dmy_dates
- 25. List::Util
reduce
F(a, b) → b, [a] --------------> b
- 26. List::MoreUtils
any, all, none, notall, true, false, firstidx,
first_index, lastidx, part, mesh,
last_index, insert_after, zip, uniq, apply,
indexes, after, after_incl, before,
before_incl, firstval, first_value, lastval,
last_value, pairwise, distinct, minmax
- 29. So how do I make functional stuff in
perl instead of just using it?
- 34. my $dx = sub {
my ($sides) = @_;
return (rand() * $sides) + 1;
}
my $roll = $dx->(6);
- 36. my $cheat = sub {
my ($roll) = @_;
if ($roll < 2) {
return 2;
} else {
return $roll;
}
}
- 37. I don't want to get caught turning the
die over though so ...
- 39. my $compose = sub {
my ($fa, $fb) = @_;
return sub {
return $fa->($fb->(@_));
}
}
- 43. my $d6 = sub {
return $inconspicuous_dx->(6);
};
my $roll_a_6 = $d6->();
- 45. my $dice_tower_maker = sub {
my ($die) = @_;
return sub {
my ($rolls) = @_;
return map {
$die->()
} (1 .. $rolls);
}
}
- 46. my $dice_tower =
$dice_tower_generator->($d6);
say join(', ', $dice_tower->(20));
- 54. If it was simple in a functional
language it probably takes some
hacking to make it work in perl
- 58. Static typing
Better syntax
Blocks and closures
List comprehension
Automatic parallelisation
Lots more