Skip to content

Instantly share code, notes, and snippets.

@frivolta
Created October 11, 2021 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frivolta/d5dc58ae9e0642de4c9f2e57c5a78a4d to your computer and use it in GitHub Desktop.
Save frivolta/d5dc58ae9e0642de4c9f2e57c5a78a4d to your computer and use it in GitHub Desktop.
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