by Alex Voerman
Immutability
Lamdas
Functors
Currying
Monads
Monoids
λ
Closures
Partial Application
(world view)
Series of commands in steps
Keep state in Objects that talk to each other
Describe what you want to do
...is Coding with Pure Functions
Same inputs always return same output
No side effects
let cart = {
orders: [],
orderTotal: 0
};
const addToCart = order => {
cart.orders.push(order);
cart.orderTotal += order.price;
};
addToCart({ name: 'tacos', price: 2.50 });
let cart = {
orders: [],
orderTotal: 0
};
const addToCart = (order, cartState) => {
cartState.orders.push(order);
cartState.orderTotal += order.price;
return cartState; // pure functions should return something
};
addToCart({ name: 'tacos', price: 2.50 }, cart);
let cart = {
orders: [],
orderTotal: 0
};
const addToCart = (order, cartState) => {
// return a new object
return {
orders: cartState.push(order),
orderTotal: cartState.orderTotal + order.price
};
};
addToCart({ name: 'tacos', price: 2.50 }, cart);
let cart = {
orders: [],
orderTotal: 0
};
const addToCart = (order, cartState) => {
return {
orders: [...cartState.orders, order], // <-- copies the array
orderTotal: cartState.orderTotal + order.price
};
};
addToCart({ name: 'tacos', price: 2.50 }, cart);
let cart = {
orders: [],
orderTotal: 0,
startedDate: new Date(),
userId: 10 // etc
};
const addToCart = (order, cartState) => {
return {
...cartState, // <-- copies the cart object to new obj
orders: [...cartState.orders, order],
orderTotal: cartState.orderTotal + order.price
};
};
addToCart({ name: 'tacos', price: 2.50 }, cart);
Arrays
Testable
Predictable
Readable
Reusable
Debuggable
and higher order functions
and hardcore "real" functional programming
Ramda
Functional Programming