Chapter 05: Coding by Composing
The book heavily relies on a function called compose
which combines functions Right -> Left
. fp-ts
provides a similar way to compose functions via flow
except that it operates on functions from Left -> Right
.
const toUpperCase = x => x.toUpperCase();
const exclaim = x => `${x}!`;
const shout = compose(exclaim, toUpperCase);
shout('send in the clowns'); // "SEND IN THE CLOWNS!"
const dasherize = compose(
intercalate('-'),
map(toLower),
split(' '),
replace(/\s{2,}/ig, ' '),
);
dasherize('The world is a vampire'); // 'the-world-is-a-vampire'
Pipe
fp-ts
also provides a function called pipe
which operates in a similar manor, except the first parameter acts as the input:
let output = pipe(
'send in the clowns',
toUpperCase,
exclaim
); // "SEND IN THE CLOWNS!"
Last updated
Was this helpful?