fazlurr
8/30/2017 - 9:15 AM

Function Composition - https://medium.com/javascript-scene/master-the-javascript-interview-what-is-function-composition-20dfb109a1a0

// From https://medium.com/javascript-scene/master-the-javascript-interview-what-is-function-composition-20dfb109a1a0

const curry = fn => (...args) => fn.bind(null, ...args);

const map = curry((fn, arr) => arr.map(fn));

const join = curry((str, arr) => arr.join(str));

const toLowerCase = str => str.toLowerCase();

const split = curry((splitOn, str) => str.split(splitOn));

const trace = curry((label, x) => {
  console.log(`== ${ label }:  ${ x }`);
  return x;
});


const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);

const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);

const fn1 = s => s.toLowerCase();
const fn2 = s => s.split('').reverse().join('');
const fn3 = s => s + '!'

const newFunc = pipe(fn1, fn2, fn3);
const result = newFunc('Time'); // emit!
console.log(result);

// const toSlug = compose(
//   encodeURIComponent,
//   join('-'),
//   map(toLowerCase),
//   split(' ')
// );

const toSlug = pipe(
  trace('input'),
  split(' '),
  map(toLowerCase),
  trace('after map'),
  join('-'),
  encodeURIComponent
);

console.log(toSlug('JS Cheerleader')); // 'js-cheerleader'