Last active
February 18, 2018 09:51
-
-
Save Baudin999/8265ab2fc3a23271cfe3ba25b4f6f6c0 to your computer and use it in GitHub Desktop.
Simple Functional Programming Examples
This file contains 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 log = (arg) => { | |
console.log(arg); | |
return arg; | |
} | |
const pipe = (...functions) => (...args) => { | |
if (!Array.isArray(functions)) { | |
return functions(args); | |
} | |
else { | |
return functions | |
.reduceRight( | |
(...arguments, f) => | |
Array.isArray(arguments) ? | |
f(...arguments) : | |
f(arguments) | |
, args) | |
} | |
} | |
const add = (x, y) => x + y; | |
const double = x => x + x; | |
const addAndDouble = pipe(double, add); | |
log(addAndDouble(12, 23)); |
This file contains 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 log = (arg) => { | |
console.log(arg); | |
return arg; | |
} | |
const pipe = (...functions) => (...args) => { | |
if (!Array.isArray(functions)) { | |
return functions(args); | |
} | |
else { | |
return functions | |
.reduce( | |
(...arguments, f) => | |
Array.isArray(arguments) ? | |
f(...arguments) : | |
f(arguments) | |
, args) | |
} | |
} | |
const add = (x, y) => x + y; | |
const double = x => x + x; | |
const addAndDouble = pipe(add, double); | |
log(addAndDouble(12, 23)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment