Skip to content

Instantly share code, notes, and snippets.

@Baudin999
Last active February 18, 2018 09:51
Show Gist options
  • Save Baudin999/8265ab2fc3a23271cfe3ba25b4f6f6c0 to your computer and use it in GitHub Desktop.
Save Baudin999/8265ab2fc3a23271cfe3ba25b4f6f6c0 to your computer and use it in GitHub Desktop.
Simple Functional Programming Examples
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));
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