Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active January 13, 2017 06:28
Show Gist options
  • Save abiodun0/b9420b21b59e8cc2dc03b7e3e84cabd6 to your computer and use it in GitHub Desktop.
Save abiodun0/b9420b21b59e8cc2dc03b7e3e84cabd6 to your computer and use it in GitHub Desktop.
Functional Compose
const map = f => arr => arr.map(f);
const compose = (f, g) => x => f(g(x));
const add = x => y => x + y;
const nestedData = [[1, 2, 3], [4, 5, 6]];
const nestedData2 = [[[1, 2, 3], [4 ,5, 6], [7, 8, 9]], [[1, 2, 3], [4 ,5, 6], [7, 8, 9]], [[1, 2, 3], [4 ,5, 6], [7, 8, 9]]];
console.log(compose(map, map)(add(1))(nestedData));
console.log(compose(compose(map, map), map)(add(1))(nestedData2));
// some factor to the function
const curriedAdd = x => y => {console.log('add.', x,'+', y, ': ', x + y); return x + y;};
const curriedSub = x => y => {console.log('sub.', x,'-', y, ': ', x - y); return x - y;};
const compose = (f, g) => x => f(g(x));
const flip = f => x => y => f(y)(x);
const curriedSubAdd = compose(curriedAdd(1), flip(curriedSub)(1));
console.log(curriedSubAdd(3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment