An Introduction to the Functional Paradigm
By Captain James T. Kirk
By Zach Fedor
But first,
Knowledge gained from driving one car is easily transferred to another with minimal inconvenience.
noun /ˈperəˌdīm/
a typical example of something; a model
So our paradigm is useless:
Next up,
Motto:
"The journey of a thousand miles begins with one step"
Building Block:
The How Statement
Languages:
Java, PHP, Python, Ruby
Motto:
"I accept reality and dare not question it"
Building Block:
The What Statement
Languages:
CSS, SQL, Regular Expressions
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.
Motto:
"Imitation ... is the sincerest form of learning"
Building Block:
The Object
Languages:
Java, PHP, Python, Ruby
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
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:
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:
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:
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:
Non-Functional Code:
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>
Summary:
@zachfedor
(if you need any directions)