Jason Adams
Senior engineer at Impress.org with a love for mentoring and coaching growing devs, coming up with simple solutions to complex problems, and hanging out with my wife!
Let's Do Some Stuff Learnin!
Interactive Git
with
Nathan Knowler
Even More Some Stuff Learnin!
Functional
Programming
A function that returns the same output given the same parameters. Also, no side effects.
let number = 0;
function addUp(a) {
number += a;
return number;
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function add(a,b) {
return a + b;
}
function setUpHook() {
addFilter('filter', function() {});
}
import React, {useState} from 'react';
function Alert({heading, content}) {
return (
<div className="alert">
<h3>{heading}</h3>
<p>{content}</p>
</div>
);
}
function AlertToggler() {
const [alertVisible, setAlertVisible] = useState(false);
return (
<>
{
alertVisible &&
<Alert heading="Oh no!" content="Do not panic. Just click the button" />
}
<button onClick={() => setAlertVisible(!alertVisible)}>Click Me</button>
</>
);
}
A function is an entity that can both be called and passed around.
Higher Order Function:
const addTen = function(a) {
return a + 10;
}
const addTwentyMore = function(f, a) {
return f(a) + 20;
}
const numbersPlusThirty = [1,2,3].map(addTwentyMore);
A variable, once set, is never changed.
let person = {
firstName: 'Jason',
lastName: 'Adams',
};
function setFirstName(person, newName) {
person.firstName = newName;
return person;
}
let person = {
firstName: 'Jason',
lastName: 'Adams',
};
function setLastName(person, newName) {
return {
...person,
lastName: newName
}
}
A function can be replaced with its output the effect remains exactly the same.
In short: Pure Function + Immutable Data = Referential Transparency
const square = a => a^2;
const describe = `The square of 4 is ${square(4)}`;
const same = 'The square of 4 is 16';
Ask yourself: Can I memoize this function?
A function can be created from an expression given its arguments
const add = function(a, b) { return a + b }
const subtract = (a, b) => a - b;
A technique wherein a function outputs a chain of functions broken down by its arguments
// const curry = f => a => b => f(a,b);
function curry(f) {
return function(a) {
return function(b) {
return f(a,b);
}
}
}
const sum = (a,b) => a + b;
const curriedSum = curry(sum);
curriedSum(4)(5); // 9
const addTen = a => curriedSum(a)(10);
addTen(5); // 15
function sumUpTo(value) {
if ( value === 0 ) {
return 0;
}
return value + sumUpTo(value - 1);
}
sumUpTo(5); // 15
A technique wherein a calculation is the result of a self-referencing function
Wherein the recursion depends on the call stack. Each stack represents that value.
sumUpTo(5)
5 + sumUpTo(4)
5 + (4 + sumUpTo(3))
5 + (4 + (3 + sumUpTo(2)))
5 + (4 + (3 + (2 + sumUpTo(1))))
5 + (4 + (3 + (2 + (1 + sumUpTo(0)))))
5 + (4 + (3 + (2 + (1 + 0))))
> 15
function sumUpTo(value, accumulator = 0) {
if ( value === 0 ) {
return accumulator;
}
return sumUpTo(value - 1, accumulator + value)
}
sumUpTo(5); // 15
A technique wherein a calculation is the result of a self-referencing function
Wherein the recursion depends on the call stack. Each stack represents that value.
sumUpTo(5, 0)
sumUpTo(4, 5)
sumUpTo(3, 9)
sumUpTo(2, 12)
sumUpTo(1, 14)
sumUpTo(0, 15)
By Jason Adams
Senior engineer at Impress.org with a love for mentoring and coaching growing devs, coming up with simple solutions to complex problems, and hanging out with my wife!