Skip to content

Commit 24fc3ad

Browse files
committed
Adding compose
1 parent 53e7efd commit 24fc3ad

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A Cookbook for writing FP in JavaScript using ES6
77
* [Higher-order functions](#higher-order-functions)
88
* [Recursion](#recursion)
99
* [Functor](#functor)
10+
* [Compose](#compose)
1011
* [Destructuring](#destructuring)
1112
* [Currying](#currying)
1213

@@ -165,6 +166,38 @@ numbers.map(plus1);
165166
// [2, 3, 4]
166167
```
167168

169+
### Compose
170+
The composition of two or more functions returns a new function.
171+
172+
1) Combining two functions to generate another one
173+
174+
```javascript
175+
let compose = (f,g) => x => f(g(x));
176+
177+
let toUpperCase = function(x) { return x.toUpperCase(); };
178+
let exclaim = function(x) { return x + '!'; };
179+
180+
let angry = compose(exclaim, toUpperCase);
181+
182+
angry("stop this");
183+
// STOP THIS!
184+
```
185+
186+
2) Combining three functions to generate another one
187+
188+
```javascript
189+
let compose = (f,g) => x => f(g(x));
190+
191+
let toUpperCase = function(x) { return x.toUpperCase(); };
192+
let exclaim = function(x) { return x + '!'; };
193+
let moreExclaim = function(x) { return x + '!!!!!'; };
194+
195+
let reallyAngry = compose(exclaim, (toUpperCase, moreExclaim));
196+
197+
reallyAngry("stop this");
198+
// STOP THIS!!!!!!
199+
```
200+
168201
### Destructuring
169202
Extract data from arrays or objects using a syntax that mirrors the construction of array and object literals. Or "Pattern Matching".
170203

@@ -208,10 +241,7 @@ Taking a function that takes multiple arguments and turning it into a chain of f
208241
1) Currying an Object
209242

210243
```javascript
211-
let student =
212-
name =>
213-
grade =>
214-
`Name: ${name} | Grade: ${grade}`;
244+
let student = name => grade => `Name: ${name} | Grade: ${grade}`;
215245

216246
student("Matt")(8);
217247
// Name: Matt | Grade: 8
@@ -220,10 +250,7 @@ student("Matt")(8);
220250
2) Currying a Sum
221251

222252
```javascript
223-
let currySum =
224-
x =>
225-
y =>
226-
x + y;
253+
let currySum = x => y => x + y;
227254

228255
let addFive = currySum(5);
229256
addFive(10);

examples.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ let plus1 = (num) => {
8080
let numbers = [1, 2, 3];
8181
console.log(numbers.map(plus1));
8282

83+
// compose
84+
85+
let compose = function(f,g) {
86+
return function(x) {
87+
return f(g(x));
88+
};
89+
};
90+
91+
let toUpperCase = function(x) { return x.toUpperCase(); };
92+
let exclaim = function(x) { return x + '!'; };
93+
let angry = compose(exclaim, (toUpperCase));
94+
95+
console.log(angry("send in the clowns"));
96+
97+
8398
// Destructuring
8499
let foo = () => {
85100
return [1, 2, 3];

0 commit comments

Comments
 (0)