Easy to test
Easy to read and reason about
Avoid confusing problems and errors in code
Really fun to write
Da future man!
Unidirectional Data Flow
Single Source of Truth
Text
Had to run code imperatively...and eventually OOP was born and took hold.
Text
React and Redux are bringing functional ideas back to the modern web
Text
These are are "Tools" to help provide pure I/O, avoid side effects, etc.
So what is a functional "language"
Functional languages simply provide tools, frameworks, best practices, etc. to "enforce" functional ideas more strictly.
A Functional method is "Pure"
// Pure Functions
function square(x) {
return x * x;
}
function squareAll(items) {
return items.map(square);
}
// Impure Functions
function square(x) {
updateXInDatabase(x);
return x * x;
}
function squareAll(items) {
for (let i = 0; i < items.length; i++) {
items[i] = square(items[i]);
}
}
// Instructor: What's 5 plus 2?
numberFive().plus(numberTwo());
// Student: 7?
// Instructor: Correct! What's 5 plus 3?
// Student: 8?
// Instructor: Wrong! It's 10,
// because we turned 5 into 7, remember?
const addUser = (user) => {
Database.addUser(user);
userCount + 1;
NotifyComponent.createToast(`User ${user.name} added!`);
};
// square :: (Number, Number) -> Number
const square = (x, y) => {
return x * y;
};
// getProgramAt :: (Guide, Channel, Date) -> Program
const getProgramAt(tvGuide, channel, time) {
const schedule = tvGuide.getSchedule(channel);
return schedule.programAt(time);
}
const getCurrentProgram(tvGuide, channel) {
const schedule = tvGuide.getSchedule(channel);
return schedule.programAt(new Date());
}
Text
A stream?
It's an ongoing set of events ordered in time. IE:
time
= a click event (for example)
runDomChangeCode();
// Dom updates
runAnotherChange();
// Dom updates
someAnimation();
// Dom updates
browserResize();
// Dom updates
runDomChangeCode();
runAnotherChange();
someAnimation();
browserResize();
// All data stored in one object (shadow dom)
React.render();
// Dom is updated all at once
Text