rogerxu / rogerxu.github.io

Roger Xu's Blog
3 stars 2 forks source link

Curry Function #72

Open rogerxu opened 7 years ago

rogerxu commented 7 years ago

A Beginner's Guide to Currying in Functional JavaScript

rogerxu commented 7 years ago

Function Composition

const toSlug = input => encodeURIComponent(
  input.split(' ')
    .map(str => str.toLowerCase())
    .join('-')
);
const toSlug = input => encodeURIComponent(
  join('-')(
    map(toLowerCase)(
      split(' ')(
        input
      )
    )
  )
);

Curry or Partial Application? – JavaScript Scene – Medium

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));