FP Study Group
Overview of Functional Programming
What Makes a Language “Functional”
Higher Order Functions
Purity
Immutable Data
Referential Transparency
Lazy Evaluation
Control of Side-Effects
Recursion
Benefits
How you think
programming functionally forces you to focus on the abstraction
History
Lamda Calculus
file+emacs:~/Dropbox/Org/notes/lamda-conf/lamda-calc.org::*combinators
Professor Frisby’s Guide
Seagull Example
Algebraic Rules
Associative Property (of addition)
X + (Y + Z) === (X + Y) + Z
Commutative Property (of addition)
X + Y === Y + X
Identity
X + 0 === X
Distributive Property
X * (Y + Z) === XY + XZ
First Class Functions
This section only covers (1) how in JavaScript function wrapping is often redundant, and (2) that you sometimes need to use =bind= when passing functions around It does not address the fundamental confusion of procedures and functions in JavaScript
Purity
procedures
and funnctions
Confusion in JavaScript between Aarray.slice
vs. Array.splice
Example of slice is pure
splice mutatates
What is a pure function?
a pure function is a mathematical function
What is a mathematical function?
just a relation between two values
“Functions can be described as a set of pairs with the position (input, output)
myFunction[input]
would be equivalent to myFunction(input)
, where in the first myFunction
is an object of key-value pairs and in the second it is actually a function that implements the mathematical relation between input and output.
you can model a function by a table, and the Javascript notation Practicalities of Purity
function output can always be cached based on input
portable (everything you need to know is in its argument and its internals)
testable (since you’re forced to inject dependencies, you can mock them easily)
referential transparency
“A spot of code is referentially transparent when it can be substituted for its evaluated value without changing the behavior of the program.”
parallelization
“we can run any pure function in parallel since it does not need access to shared memory and it cannot, by definition, have a race condition due to some side effect”
Making Pure Functions
sometimes you can conver impure function into pure function by “delaying execution”
this really just means separating the concept of an action from its execution; the action is a value and can be passed around as a function; the execution or procedure is its side effect