This article is part of the Writing Software series about functional programming and various techniques for writing JavaScript ES6 + programs, starting with the basics. Stay connected, a lot of new things to come!
Composition: "An action consisting in making a whole from parts or elements."
In my first programming lesson in high school, I was told that software development is "breaking down complex problems into their constituent parts, and then combining simple solutions into complex ones to solve the original problem."
Most of all in my life I regret that I did not understand the full importance of this lesson in the very beginning. The essence of software development became clear to me too late.
I have conducted hundreds of interviews with programmers, and this has brought me the understanding that I am not alone. Very few programmers really grasp the essence of the software development process. They are not aware of the most important tools at our disposal, or they simply do not know how to use them correctly. Absolutely everyone had difficulty answering one or both questions about the most important things in development:
What is functional composition (function composition)?
What is object composition (composition of objects)?
, , . - . , , , . . . , - , .
. - , . 2013 Toyota " " , .. "-" 10000 .
, , , , DDoS , .
.
, , . (, , ), , , .
, , .
. : f
g
, (f ∘ g)(x) = f(g(x))
. ∘
- . , " f
g
f
g
x
". , f
, g
, f
g
.
, , :
const g = n => n + 1;
const f = n => n * 2;
const doStuff = x => {
const afterG = g(x);
const afterF = f(afterG);
return afterF;
};
doStuff(20); // 42
, promise
, :
const g = n => n + 1;
const f = n => n * 2;
Promise.resolve(20)
.then(g)
.then(f)
.then(value => console.log(value)); // 42
, , (map
, filter
, etc), lodash
, observables
RxJS
, . - . - . - this
.
- . doStuff()
:
const g = n => n + 1;
const f = n => n * 2;
const doStuffBetter = x => f(g(x));
doStuffBetter(20); // 42
, "after g" trace()
:
const trace = label => value => {
console.log(`${ label }: ${ value }`);
return value;
};
:
const doStuff = x => {
const afterG = g(x);
trace('after g')(afterG);
const afterF = f(afterG);
trace('after f')(afterF);
return afterF;
};
doStuff(20); // =>
/*
"after g: 21"
"after f: 42"
*/
Lodash Ramda , . :
import pipe from 'lodash/fp/flow';
const doStuffBetter = pipe(
g,
trace('after g'),
f,
trace('after f')
);
doStuffBetter(20); // =>
/*
"after g: 21"
"after f: 42"
*/
, pipe
:
// pipe(...fns: [...Function]) => x => y
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
, , . . , , . , , . .
pipe()
, , . pipe()
( compose()
) . () . , , , , . , function
=>
( doStuffBetter).
, , .
:
. , . , , . 4 7 . 7, . , 3- , . . , , , , , , .
-. , " -". , . , . . , , . - , - . , , .
. . , . , , , .
" " - , " - . "
" - , . […] ." - Wikipedia
:
const firstName = 'Claude';
const lastName = 'Debussy';
:
const fullName = {
firstName,
lastName
};
Array
, Set
, Map
, WeakMap
, TypedArray
. , - .
, " " , "" (Composite), , . , "" . , .
" " : " ", , (, , "", "", ""), ( , , "", , , - ) ( , , "", , , DOM- ).
, . " " " ", " " , , .
, , , " : " (1989):
, "" .
" " (1975). , Amazon Ebay, .
. , . " " , , , . - :
: ,
: - , . , ,
: , ,
: , , . , .
: " - , , . , , " - , " ".
JavaScript ( ). , . (), . , , - .
:
class Foo {
constructor () {
this.a = 'a'
}
}
class Bar extends Foo {
constructor (options) {
super(options);
this.b = 'b'
}
}
const myBar = new Bar(); // {a: 'a', b: 'b'}
:
const a = {
a: 'a'
};
const b = {
b: 'b'
};
const c = {...a, ...b}; // {a: 'a', b: 'b'}
. , , :
, .
, , () - () , . , , , .. . Java , Haskell - , .. , , . .
, JavaScipt , .
, , , , . - . JavaScript , , .
, .
, , .
, , , , . .
Now is the time for simplification, and the best way to do this is to understand the essence. The problem, however, is that almost no one in the industry understands the basics. We, the industry, have let you down as a developer. It is our responsibility as an industry to educate programmers better. We must fix ourselves. We need to take more responsibility. Everything around today depends on software, from the economy to medical equipment. There are literally no places in human life that are not affected by the quality of software. We need to know what we are doing.
Now is the time to learn how to develop software.