-
Notifications
You must be signed in to change notification settings - Fork 1
/
composition.js
28 lines (20 loc) · 964 Bytes
/
composition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Composition
// Combining multiple functions so the output is chained
const scream = str => str.toUpperCase();
const exclaim = str => `${str}!`;
const repeat = str => `${str} ${str}`;
// Create compose function
// Take a list of function and compose them in reverse (so its readable) and make
// a new function from it.
const compose = (...fns) => x => fns.reduceRight((accumulator, fn) => fn(accumulator), x);
const partiallyExcited = compose(exclaim);
const veryExcited = compose(scream, partiallyExcited);
const uberExcited = compose(repeat, veryExcited);
console.log('partially excited', partiallyExcited('Oddball Rocks'));
//Oddball Rocks!
console.log('very excited', veryExcited('Oddball Rocks'));
//ODDBALL ROCKS!
console.log('uber excited', uberExcited('Oddball Rocks'));
// ODDBALL ROCKS! ODDBALL ROCKS!
console.log('Travis at retreat', uberExcited(uberExcited('Oddball Rocks')));
//ODDBALL ROCKS! ODDBALL ROCKS!! ODDBALL ROCKS! ODDBALL ROCKS!!