How To Fly a Spaceship
An Introduction to the Functional Paradigm
By Captain James T. Kirk
By Zach Fedor
But first,
a parable
How to Drive a Car
- they all have the same integral parts
- they all follow the same constraints
- they all serve the same purpose
Knowledge gained from driving one car is easily transferred to another with minimal inconvenience.
Paradigm
noun /ˈperəˌdīm/
a typical example of something; a model
Spaceships Aren't Cars
So our paradigm is useless:
- Steering wheel for a third dimension?
- Shift into reverse?
- Radio in lightspeed?
- We don't need roads?
Next up,
Definitions
Imperative
Motto:
"The journey of a thousand miles begins with one step"
Building Block:
The How Statement
Languages:
Java, PHP, Python, Ruby
Declarative
Motto:
"I accept reality and dare not question it"
Building Block:
The What Statement
Languages:
CSS, SQL, Regular Expressions
Some Metaphors
Go to 342 N Queen St, Lancaster, PA 17603
Can I have a Big Mac?
Walk east on King St and turn right onto Queen. Walk north four blocks and it's on your left.
Grill two beef patties, put them in between a sesame seed bun split in thirds. Add lettuce, tomato, onion, and special sauce.
Object Oriented
Motto:
"Imitation ... is the sincerest form of learning"
Building Block:
The Object
Languages:
Java, PHP, Python, Ruby
Functional
Motto:
"The universe is change; our life is what our thoughts make it"
Building Block:
The Function
Languages:
Haskell, Lisp, Scala
Now it's time for some
Code Samples
function add(x, y) {
return x + y;
}
How many inputs and outputs are there?
function saveCookie() {
if (!document.cookie) {
document.cookie = new Date();
}
}
First Pillar:
pure Functions
- do not rely on external state
- do not alter external state
- when given the same input, will always return the same output
How else can we express pure functions?
function square(x) {
return x * x;
}
square(2); // 4
square(4); // 16
square(square(2)); // 16
var square = {
1: 1,
2: 4,
3: 9,
4: 16
};
square[2]; // 4
square[4]; // 16
Look familiar?
Second Pillar:
Referential Transparency
- replacing an expression with its value will not change the result
- easy to read code
- easy to test code
What is referential transparency?
function factorial(x) {
if (x === 1) {
return x;
} else {
return x * factorial(x - 1);
}
}
factorial(4);
4 * factorial(3);
4 * 3 * factorial(2);
4 * 3 * 2 * factorial(1);
4 * 3 * 2 * 1;
4 * 3 * 2;
4 * 6;
24;
Third Pillar:
Recursion
- when a thing is defined in terms of itself
- easier to read code
- avoids mutation
function factorial(x) {
var result = 1;
for (x; x > 0; x--) {
result = result * x;
}
return result;
}
What if you didn't use the word in the definition?
factorial(4); // 24
// result = 1 * 4 = 4
// result = 4 * 3 = 12
// result = 12 * 2 = 24
// result = 24 * 1 = 24
What is so bad about re-assignment?
var count = 0;
function demo(x) {
count++;
return x + count;
}
demo(3); // 4
demo(3); // 5
demo(3) === demo(3); // false
Fourth Pillar:
Immutability
- when something is unchanging over time
- easy to avoid mistakes
- easy to test code
So What?
Non-Functional Code:
- encourages side-effects
- removes the substitution model
- encourages verbose implementation details
- adds the problem of time
Double all the numbers in an array:
var array = [1, 2, 3, 4, 5];
var doubled = [2, 4, 6, 8, 10];
Sum all the numbers in an array:
var array = [1, 2, 3, 4, 5];
var sum = 15;
Discount all the items in a shopping cart:
var items = [
{ product: 'rope', price: 140, quantity: 1 },
{ product: 'harness', price: 50, quantity: 2 },
{ product: 'carabiner', price: 9, quantity: 5 }
];
var discount = 0.2;
var discountedItems = [
{ product: 'rope', price: 112, quantity: 1 },
{ product: 'harness', price: 40, quantity: 2 },
{ product: 'carabiner', price: 7.2, quantity: 5 }
];
var total = 228;
Paginate an array of blog posts:
var posts = [
{ title: "post one", date: "2017-01-04", content: "..." },
{ title: "post two", date: "2017-01-03", content: "..." },
{ title: "post three", date: "2017-01-02", content: "..." },
{ title: "post four", date: "2017-01-01", content: "..." },
{ title: "post five", date: "2016-12-31", content: "..." }
];
var pageOne = [
{ title: "post one", date: "2017-01-04", content: "..." },
{ title: "post two", date: "2017-01-03", content: "..." },
{ title: "post three", date: "2017-01-02", content: "..." }
];
var pageTwo = [
{ title: "post four", date: "2017-01-01", content: "..." },
{ title: "post five", date: "2016-12-31", content: "..." }
];
On click, change the text and class of a button:
$('#button').click(function() {
$(this).toggleClass("active");
$(this).text() === 'Click Me'
? $(this).text('Stop Clicking Me')
: $(this).text('Click Me');
});
<Btn
onClick={this.handleClick}
color={this.state.color}>
{this.state.text}
</Btn>
Next Steps
- learn some advanced stuff
- currying
- composition
- monads
- try out a functional language like Elm
- try out a functional framework like React
- practice these concepts!
Questions?
Summary:
- Definition of the four main paradigms
- Pure functions to avoid side-effects
- Referential Transparency to make code more readable
- Recursion to avoid mutability
- Immutability to avoid problems with time
Go Fly a SpaceshiP!
@zachfedor
(if you need any directions)
How To Fly A Spaceship
By zach fedor
How To Fly A Spaceship
An Introduction to the Functional Paradigm
- 1,353