Skip to content

Instantly share code, notes, and snippets.

@kucherenko
Created January 13, 2021 11:31
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 kucherenko/136c376a7e662b125b703e951cf7e170 to your computer and use it in GitHub Desktop.
Save kucherenko/136c376a7e662b125b703e951cf7e170 to your computer and use it in GitHub Desktop.
// Compose functions from right to left
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
// Example
const lowercase = str => str.toLowerCase();
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const reverse = str => str.split('').reverse().join('');
const fn = compose(reverse, capitalize, lowercase);
// We will execute `lowercase`, `capitalize` and `reverse` in order
fn('Hello World') === 'dlrow olleH';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment