Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Last active May 9, 2018 13:30
Show Gist options
  • Save fwojciec/18adc991b565f6bb873b1de64b2f77ca to your computer and use it in GitHub Desktop.
Save fwojciec/18adc991b565f6bb873b1de64b2f77ca to your computer and use it in GitHub Desktop.
// based on https://www.youtube.com/watch?v=mth5WpEc4Qs
"use strict";
var add1 = function add1(num) {
return num + 1;
};
var doubleIt = function doubleIt(num) {
return num * 2;
};
// equivalent to (acc, cur) => {return cur(acc)}
var apply = function apply(fn1, fn2) {
return fn2(fn1);
};
// we use currying so that the function applies the array of fns, but waits on val
var compose = function compose(arr) {
return function (val) {
return arr.reduce(apply, val);
};
};
// and this is where we test this out
var add1AndDoubleIt = compose([add1, doubleIt]);
console.log(add1AndDoubleIt(2));
// based on https://www.youtube.com/watch?v=mth5WpEc4Qs
const add1 = num => num + 1
const doubleIt = num => num * 2
// equivalent to (acc, cur) => {return cur(acc)}
const apply = (fn1, fn2) => fn2(fn1)
// we use currying so that the function applies the array of fns, but waits on val
const compose = arr => val => arr.reduce(apply, val)
// and this is where we test this out
const add1AndDoubleIt = compose([add1, doubleIt])
console.log(add1AndDoubleIt(2))
// based on https://www.youtube.com/watch?v=mth5WpEc4Qs
"use strict";
var add1 = function add1(num) {
return num + 1;
};
var doubleIt = function doubleIt(num) {
return num * 2;
};
// equivalent to (acc, cur) => {return cur(acc)}
var apply = function apply(fn1, fn2) {
return fn2(fn1);
};
// we use currying so that the function applies the array of fns, but waits on val
var compose = function compose(arr) {
return function (val) {
return arr.reduce(apply, val);
};
};
// and this is where we test this out
var add1AndDoubleIt = compose([add1, doubleIt]);
console.log(add1AndDoubleIt(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment