Created
October 11, 2021 08:16
-
-
Save frivolta/d5dc58ae9e0642de4c9f2e57c5a78a4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multiply20 = (price) => price * 20; | |
const divide100 = (price) => price / 100; | |
const normalizePrice = (price) => price.toFixed(2); | |
const addPrefix = (price) => "$" + String(price); | |
const pipe = | |
(...fns) => | |
(x) => | |
fns.reduce((res, fn) => fn(res), x); | |
const compose = | |
(...fns) => | |
(x) => | |
fns.reduceRight((res, fn) => fn(res), x); | |
const discountPipe = pipe(multiply20, divide100, normalizePrice, addPrefix); | |
const discountCompose = compose( | |
addPrefix, | |
normalizePrice, | |
divide100, | |
multiply20 | |
); | |
discountPipe(200); // '$40.00' | |
discountCompose(200); // '$40.00' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment