John Sutherland: A humane introduction to functional programming
John Sutherland @sneeu was talking about functional programming at Scotland.js, these are my notes from his talk.
Systems grow over time and we mange this complexity with modularity. Functional programming gives us tools to deal with this, it's a new angle to attack programs - not just the OO hammer.
Higher-order functions
Functions which take functions as arguments. map()
is an example of this, we iterate over a list running our function that was passed in as an argument to each item.
Purity
A pure function always evaluates to the same result given the same arguments. Typically impure functions have access to variables outside their scopte. (Assuming nobody has mucked around with the prototypes.)
Immutability
An object whose value cannot be changed after it is created.
Application
Summing a list - could just iterate and sum. The +
isn't a function in JS so we need to use an add()
function which we then pass to reduce()
function, a start value and a list.
Functional programming is similar to piping command lines together, each function passes on to another function.
If our functions are pure it is possible to cache calls (_.memoise()
).