Functional Programming Made
Me a Better Perl Developer
Mark Allen | mrallen1@yahoo.com | @bytemeorg
Slide 2
Slide 2 text
Agenda
Functional Programming: What is it?
All you need is fold
Programming Perl in a Functional Style
@bytemeorg
Slide 3
Slide 3 text
What is Functional Programming?
A programming paradigm which aims
to express computation using
functions that avoid (as much as
possible) side-effects.
@bytemeorg
Slide 4
Slide 4 text
Why is that a good thing?
Make all state change explicit
All functions behave deterministically
(given a function and its inputs, it always
returns the same output)
@bytemeorg
Slide 5
Slide 5 text
How are these things manifested?
Immutable variable bindings (in a scope)
3 crucial higher order functions:
filter, map, fold (reduce)
Recursively operate on lists
@bytemeorg
Slide 6
Slide 6 text
fold right
let rec foldr f a lst = match lst with
| [] -> a
| hd::tl -> f hd (foldr f a tl);;
@bytemeorg
Slide 7
Slide 7 text
fold right example
foldr (+) 0 [1;2;3;];;
(* output: - : int = 6 *)
(* same as: f 1 (f 2 (f 3 0)) *)
(* same as: 1 + ( 2 + (3 + 0) ) = 6 *)
@bytemeorg
Slide 8
Slide 8 text
another fold right example
foldr (-) 0 [1;2;3;];;
(* what is this value? *)
@bytemeorg
Slide 9
Slide 9 text
another fold right example
foldr (-) 0 [1;2;3;];;
(* output: - int = 2 *)
(* why? 1-(2-(3-0)) = 1 - -1 = 2 *)
@bytemeorg
Slide 10
Slide 10 text
fold left
let rec foldl f a lst = match lst with
| [] -> a
| hd::tl -> foldl f (f a hd) tl;;
@bytemeorg
Slide 11
Slide 11 text
fold left example
foldl (+) 0 [1;2;3;];;
(* output: - int = 6 *)
(* same as: ((1+0)+2)+3 *)
@bytemeorg
Slide 12
Slide 12 text
another fold left example
foldl (-) 0 [1;2;3;];;
(* what is this value? *)
@bytemeorg
Slide 13
Slide 13 text
another fold left example
foldl (-) 0 [1;2;3;];;
(* output: - int = -6 *)
(* same as: ((0-1)-2)-3 *)
@bytemeorg
Slide 14
Slide 14 text
map
let map f = foldr (fun x lst-> (f x)::lst) [];;
@bytemeorg
Slide 15
Slide 15 text
map example
map (fun x-> x*2) [ 1; 2; 3; ];;
(* output: - : int list = [2; 4; 6] *)
@bytemeorg
Slide 16
Slide 16 text
map example (Erlang list comprehension)
[ X * X || X <- lists:seq(1,5) ].
% [1,4,9,16,25]
@bytemeorg
Slide 17
Slide 17 text
filter
let filter p = foldr
(fun x a -> if p x then x::a else a) [];;
@bytemeorg
Slide 18
Slide 18 text
filter example
filter (fun x -> (x mod 2) == 0) [1;2;3;4;];;
(* output: - : int list = [2;4] *)
@bytemeorg
Slide 19
Slide 19 text
filtermap example
[ X * X || X <- lists:seq(1,5), X rem 2 == 0 ].
% [4,16]
@bytemeorg
Slide 20
Slide 20 text
Higher Order Perl
http://hop.perl.plover.com/book/
@bytemeorg
Slide 21
Slide 21 text
List processing primitives in Perl
grep
map
reduce (List::Util added to core in 5.7.3)
@bytemeorg
real world example
use JSON::PP;
my $json = qq|{"foo": true,
"bar": 42,
"qux": 3.14159,
"baz": ["hoge", "wak", "jib"]}|;
my $hr = decode_json($json);
@bytemeorg
Slide 30
Slide 30 text
real world example
# make a new hash with keys that start with 'b'
my %newhr = map { $_ => $hr->{$_} }
grep {; /\Ab/ } keys %{$hr};
# also works great for DBI results
@bytemeorg
Slide 31
Slide 31 text
Thank you! Questions?
Mark Allen | mrallen1@yahoo.com | @bytemeorg
Slide 32
Slide 32 text
references
Why Functional Programming Matters, Hughes, J, 1990:
http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
Investigating fold in OCaml: https://www.matt-
mcdonnell.com/code/code_ocaml/ocaml_fold/ocaml_fold.html
Free Introduction to OCaml Online Class: https://www.fun-
mooc.fr/courses/parisdiderot/56002S02/session02/about
https://metacpan.org/pod/List::Util#reduce
All You Need is Fold (Midwest.io 2015): https://www.youtube.com/watch?v=bzHKp6p-Rlk