The two most famous higher order functions are map and reduce
"[Google's MapReduce] abstraction is inspired by the map and reduce primitives present in Lisp and many other functional languages"
- Dean and Ghemawat, 2008
Let's get started with map. Map applies a function to every item in a list, and replaces with the return value.
For example:
let double = ( * ) 2;;
let naturals = [1; 2; 3; 4; 5; 6];;
val double : int -> int = <fun>
val naturals : int list = [1; 2; 3; 4; 5; 6]
We can double each of the natural numbers in our list with a map
(* bad style! *)
List.map (fun x -> double x) naturals;;
- : int list = [2; 4; 6; 8; 10; 12]
Wait! This code has bad style! We don't need that anonymous function:
List.map double naturals;;
- : int list = [2; 4; 6; 8; 10; 12]
That's much better :)