Last active
January 13, 2017 06:28
-
-
Save abiodun0/b9420b21b59e8cc2dc03b7e3e84cabd6 to your computer and use it in GitHub Desktop.
Functional Compose
This file contains hidden or 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 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