Place with explanations for each buzzword in the JS world.😀
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Functional programming:
A:
Arrow Functions
C:
Composition
Is a concept that allows you to combine two or more functions into a new function. Composition has a companion concept: piping
.
🔎 code sample ...
const compose = (...fns) => (x) => fns.reduceRight((x, fn) => fn(x), x)
// Usage
const upperFirst = word => word.charAt(0).toUpperCase() + word.slice(1)
const upperCapital = s => s.split(' ').map(upperFirst).join(' ')
const lower = s => s.toLowerCase()
const capitalize = compose(upperCapital, lower)
const s = 'FOO BAR'
capitalize(s) // Foo Bar
Currying
Is a technique of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument. Can be implemented using partial application
.
🔎 code sample ...
const curry = fn => (...args) => {
if (fn.length > args.length) {
const f = fn.bind(null, ...args)
return curry(f)
} else {
return fn(...args)
}
}
F:
First-class Function
Functions are treated as values – you can assign a function into a variable, pass it around etc. Related terms: higher-order function
.
H:
Higher-order Function
A function that takes a function as an argument, or returns a function. Related terms: first-class function
.
I:
Immutable Data
M:
Mixins
Memoization
An optimization technique by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
P:
Partial application
Is a technique of fixing a number of arguments to a function, producing another function of smaller arguments i.e binding values of those arguments. Example: function.bind(null, [arg1], ...)
.
Piping
Is a concept that allows you to combine two or more functions into a new function.
🔎 code sample ...
const pipe = (...fns) => (x) => fns.reduce((x, fn) => fn(x), x)
// Usage
const upperFirst = word => word.charAt(0).toUpperCase() + word.slice(1)
const upperCapital = s => s.split(' ').map(upperFirst).join(' ')
const lower = s => s.toLowerCase()
const capitalize = pipe(lower, upperCapital)
const s = 'FOO BAR'
capitalize(s) // Foo Bar
Pure Function
A function where the return value is only determined by its input values, without side effects
.
S:
Side effects
W:
Wrapper
A function that wraps a function by adding new behaviour.
🔎 example of Cancelable wrapper ...
const cancelable = fn => {
const wrapper = (...args) => {
if (fn) return fn(...args);
}
wrapper.cancel = () => fn = null
return wrapper
}
// ...
const f = cancelable(fn);
f() // called
f.cancel()
f() // ignored