FP
Π€ΡΠ½ΠΊΡΠΈΠΎΠ½Π°ΜΠ»ΡΠ½ΠΎΠ΅ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΜΡΠΎΠ²Π°Π½ΠΈΠ΅ β ΡΠ°Π·Π΄Π΅Π» Π΄ΠΈΡΠΊΡΠ΅ΡΠ½ΠΎΠΉ ΠΌΠ°ΡΠ΅ΠΌΠ°ΡΠΈΠΊΠΈ ΠΈ ΠΏΠ°ΡΠ°Π΄ΠΈΠ³ΠΌΠ° ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ, Π² ΠΊΠΎΡΠΎΡΠΎΠΉ ΠΏΡΠΎΡΠ΅ΡΡ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ ΡΡΠ°ΠΊΡΡΠ΅ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΡΡΠ½ΠΊΡΠΈΠΉ Π² ΠΌΠ°ΡΠ΅ΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΏΠΎΠ½ΠΈΠΌΠ°Π½ΠΈΠΈ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΡ (Π² ΠΎΡΠ»ΠΈΡΠΈΠ΅ ΠΎΡ ΡΡΠ½ΠΊΡΠΈΠΉ ΠΊΠ°ΠΊ ΠΏΠΎΠ΄ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌ Π² ΠΏΡΠΎΡΠ΅Π΄ΡΡΠ½ΠΎΠΌ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ).
ΠΡΠΎΡΠΈΠ²ΠΎΠΏΠΎΡΡΠ°Π²Π»ΡΠ΅ΡΡΡ ΠΏΠ°ΡΠ°Π΄ΠΈΠ³ΠΌΠ΅ ΠΈΠΌΠΏΠ΅ΡΠ°ΡΠΈΠ²Π½ΠΎΠ³ΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΎΠΏΠΈΡΡΠ²Π°Π΅Ρ ΠΏΡΠΎΡΠ΅ΡΡ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠΉ ΠΊΠ°ΠΊ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠΎΡΡΠΎΡΠ½ΠΈΠΉ (Π² Π·Π½Π°ΡΠ΅Π½ΠΈΠΈ, ΠΏΠΎΠ΄ΠΎΠ±Π½ΠΎΠΌ ΡΠ°ΠΊΠΎΠ²ΠΎΠΌΡ Π² ΡΠ΅ΠΎΡΠΈΠΈ Π°Π²ΡΠΎΠΌΠ°ΡΠΎΠ²). ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ, Π² ΡΡΠ½ΠΊΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎΠΌ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ Π²ΡΡ ΡΠΎΠ²ΠΎΠΊΡΠΏΠ½ΠΎΡΡΡ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠΉ Π²ΡΡΠΈΡΠ»ΠΈΡΠ΅Π»ΡΠ½ΠΎΠ³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ° ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΠ΅ΡΡΡ ΡΠ²Π½ΡΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΊΠ°ΠΊ ΡΠΏΠΈΡΠΎΠΊ.
Π€ΡΠ½ΠΊΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎΠ΅ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅Ρ ΠΎΠ±Ρ ΠΎΠ΄ΠΈΡΡΡΡ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΡΡΠ½ΠΊΡΠΈΠΉ ΠΎΡ ΠΈΡΡ ΠΎΠ΄Π½ΡΡ Π΄Π°Π½Π½ΡΡ ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ ΡΡΠ½ΠΊΡΠΈΠΉ, ΠΈ Π½Π΅ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅Ρ ΡΠ²Π½ΠΎΠ³ΠΎ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ. Π‘ΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ, Π½Π΅ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅Ρ ΠΎΠ½ΠΎ ΠΈ ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΠΌΠΎΡΡΡ ΡΡΠΎΠ³ΠΎ ΡΠΎΡΡΠΎΡΠ½ΠΈΡ (Π² ΠΎΡΠ»ΠΈΡΠΈΠ΅ ΠΎΡ ΠΈΠΌΠΏΠ΅ΡΠ°ΡΠΈΠ²Π½ΠΎΠ³ΠΎ, Π³Π΄Π΅ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· Π±Π°Π·ΠΎΠ²ΡΡ ΠΊΠΎΠ½ΡΠ΅ΠΏΡΠΈΠΉ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ, Ρ ΡΠ°Π½ΡΡΠ°Ρ ΡΠ²ΠΎΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΡΡΠ°Ρ ΠΌΠ΅Π½ΡΡΡ Π΅Π³ΠΎ ΠΏΠΎ ΠΌΠ΅ΡΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π°Π»Π³ΠΎΡΠΈΡΠΌΠ°).
HOF - High ordered function
In mathematics and computer science, a higher-order function is a function that does at least one of the following:
takes one or more functions as arguments (i.e. procedural parameters),
returns a function as its result.
const hof = (a) => (b) => a + b;
const repeat = (n, action) => {
for (let i = 0; i < n; i++) {
action(i);
}
}
repeat(3, console.log);
Partial application (Π§Π°ΡΡΠΈΡΠ½ΠΎΠ΅ ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅)
Is a technique of fixing a number of arguments to a function, producing another function of smaller arguments i.e binding values to one or more of those arguments as the chain of function progressed.
JavaScript has the built-in method .bind
that works on functions with any number of arguments and can bind an arbitrary amount of parameters. Its invocation has the following syntax.
function.bind(context, [arg1], [arg2], ...)
It turns function into a new function whose implicit this parameter is this value and whose initial arguments are always as given.
function addition(x, y) {
return x + y;
}
const plus5 = addition.bind(null, 5)
plus5(10) // output -> 15
//es6 pure partial
const partial = (fn, ...args) => (...rest) => fn(...args, ...rest)
Composition
ΠΠΎΠΌΠΏΠΎΠ·ΠΈΜΡΠΈΡ ΡΡΜΠ½ΠΊΡΠΈΠΉ (ΠΈΠ»ΠΈ ΡΡΠΏΠ΅ΡΠΏΠΎΠ·ΠΈΜΡΠΈΡ ΡΡΜΠ½ΠΊΡΠΈΠΉ) β ΡΡΠΎ ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΎΠ΄Π½ΠΎΠΉ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΊ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π΄ΡΡΠ³ΠΎΠΉ.
const compose = (f1, f2) => value => f1( f2(value))
const number = compose(Math.round, parseFloat);
number('72.5') // 73
Curry
A curried function is a function that takes multiple arguments one at a time.
Curried functions on the other hand always return a unary function: a function which takes one argument.
All curried functions return partial applications, but not all partial applications are the result of curried functions.
The unary requirement for curried functions is an important feature.
// add = a => b => Number
const add = a => b => a + b;
const result = add(2)(3); // => 5
Clear functions
Memoize
Clojure
Links
Last updated
Was this helpful?