x
const x
λx.x
x => x; // or function(x) { return x }
λx.x²
x => x ** 2;
λx.λy.x
function (x) { return function(y) { return x } }
doesn't matter what the variable is called
λx.x 10
(x => x)(10); // or (function(x) { return x })(10)
λx.x²(y)
const SQUARE = x => x ** 2; SQUARE(x)
λx.λy.x
λx.λy.y
TRUE
FALSE
So how would we do IF
λz.λx.λy.z x y
const IF = COND => THEN => ELSE => COND(THEN)(ELSE);
export function noop() {}
export function id(x) {
return x
}
export function constant(value) {
return () => value
}
export function unary(fn) {
return a => fn(a)
}
export function binary(fn) {
return (a, b) => fn(a, b)
}
export function apply(fn) {
return args => fn(...args)
}
Create some functional functions
Learn More about functional programming
References for these slides
const NEXT_MOVE = {
WORK: 'HOME',
HOME: 'BED',
BED: 'HOME'
}
// ...
let currentMove = 'WORK';
NEXT_MOVE[CURRENT_MOVE] // 'HOME'
NEXT_MOVE[NEXT_MOVE[CURRENT_MOVE]] // 'BED'
// ...
let currentMove = 'WORK';
if (currentMove == 'WORK') {
currentMove = 'BED'
}
// NEXT_MOVE[NEXT_MOVE[CURRENT_MOVE]]
// is more expressive and shorter
const STRATEGY = {
WORK: () => { ... },
HOME: () => { ... },
BED: () => { ... },
DEFAULT: () => { ... },
}
const currentState = 'WORK'
(STRATEGY[currentState] || STRATEGY.DEFAULT)()