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 plusOne = a => a + 1; | |
| const plusTwo = a => a + 2; | |
| const composedPlusThree = a => plusTwo(plusOne(a)); | |
| composedPlusThree(3); // 6 |
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 compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x); | |
| const composePipe = (...fns) => x => fns.reduce((v, f) => f(v), x); |
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 plusA = s => s + 'A'; | |
| const plusB = s => s + 'B'; | |
| const composed1 = s => compose(plusA, plusB)(s); | |
| const composed2 = s => composePipe(plusA, plusB)(s); | |
| composed1(''); // BA | |
| composed2(''); // AB |
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 composedPointFree = compose(plusA, plusB); | |
| console.log(composedPointFree('') === composed1('')); // true |
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
| function simonSays(arg) { | |
| let result = arg.trim(); | |
| result = `Simon Says: ${result}`; | |
| return result; | |
| } | |
| simonSays(' Jump! '); // Simon Says: Jump! |
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 trim = a => a.trim(); | |
| const add = a => b => a + b; | |
| const simonSays = composePipe(trim, add('Simon Says: ')); | |
| simonSays(' Jump! '); // Simon Says: Jump! |
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 add = a => b => a + b; | |
| const partialSimonSays = add('Simon Says:'); // partial application | |
| const simonSays = composePipe(trim, partialSimonSays); | |
| partialSimonSays('Jump!'); // Simon Says: Jump! | |
| simonSays(' Jump! '); // Simon Says: Jump! |
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
| // console.log is impure and does not provide any return value | |
| // so we have to improve it | |
| const investigate = a => console.log(a) || a; | |
| const simonSays = composePipe( | |
| investigate, | |
| trim, | |
| investigate, | |
| partialSimonSays, | |
| investigate |
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
| import {bark} from './dog'; | |
| import {compose} from './functional'; | |
| const doubleIt = a => 2 * a; | |
| export const roboBark = composePipe(bark, doubleIt); |
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 arrowFunction = (arg1, arg2) => arg1 + arg 2; | |
| const traditionalFunction = function(arg1, arg2) { | |
| return arg1 + arg2; | |
| }; |