Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Thinking in ES6

Functional Thinking in ES6

Danielle Brook-Roberge

August 23, 2016
Tweet

More Decks by Danielle Brook-Roberge

Other Decks in Programming

Transcript

  1. (sum (if doFour 4 1) 2 3) -> 6 if

    doFour is false, -> 9 if doFour is true Mobify 13
  2. ((if doProduct prod sum) 1 2 3 4) -> 10

    if doProduct is false, -> 24 if doProduct is true Mobify 14
  3. Func%on as Argument Scheme's symmetry makes it clear that the

    func3on in a call is as much of an input as any of the arguments. One might even say that the func1on is an argument to the "func1on call" opera1on, just as the func1on's arguments are. Mobify 15
  4. Pipelines: Composi-on from the Inside Out If we look at

    composi0on in the opposite order, it becomes a pipeline. The innermost composed func0on is the first to receive the data, and it propagates from the inside to the outside. Two common pa*erns in Javascript act as pipelines: Array methods and Promise chains. Mobify 25
  5. fs.readFileAsync('details.json', 'utf8') .then((contents) => { const details = JSON.parse(contents) const

    stableDepUrls = [] for (const dep of details.dependencies) { if (!dep.version.startsWith('0')) { dep.url = dep.url.replace('http:', 'https:') stableDepUrls.push(dep.url) } } const payload = JSON.stringify({urls: stableDepUrls}) fs.writeFileAsync('stable-urls.json', payload, 'utf8') .then(() => console.log('Done')) }) Mobify 29
  6. fs.readFileAsync('details.json', 'utf8') .then(JSON.parse) .then((details) => { const stableDepUrls = []

    for (const dep of details.dependencies) { if (!dep.version.startsWith('0')) { dep.url = dep.url.replace('http:', 'https:') stableDepUrls.push(dep.url) } } const payload = JSON.stringify({urls: stableDepUrls}) return fs.writeFileAsync('stable-urls.json', payload, 'utf8') }) .then(() => console.log('Done')) Mobify 30
  7. fs.readFileAsync('details.json', 'utf8') .then(JSON.parse) .then((details) => { const stableDepUrls = []

    for (const dep of details.dependencies) { if (!dep.version.startsWith('0')) { dep.url = dep.url.replace('http:', 'https:') stableDepUrls.push(dep.url) } } return stableDepUrls }) .then((urls)) => { const payload = JSON.stringify({urls}) return fs.writeFileAsync('stable-urls.json', payload, 'utf8') }) .then(() => console.log('Done')) Mobify 31
  8. fs.readFileAsync('details.json', 'utf8') .then(JSON.parse) .then((details) => details.dependencies.filter( (dep) => dep.version.startsWith(0)) )

    .then((stableDeps) => stableDeps.map( (dep) => dep.url.replace('http:', 'https:')) ) .then((urls)) => { const payload = JSON.stringify({urls}) return fs.writeFileAsync('stable-urls.json', payload, 'utf8') }) .then(() => console.log('Done')) Mobify 32
  9. fs.readFileAsync('details.json', 'utf8') .then(JSON.parse) .then((details) => details.dependencies) .then((deps) => deps.filter((dep) =>

    !dep.version.startsWith('0'))) .then((stableDeps) => _.pluck(deps, 'url')) .then((depUrls) => depUrls.map((url) => url.replace('http:', 'https:'))) .then((urls) => JSON.stringify({urls})) .then((json) => fs.writeFileAsync('stable-urls.json', json, 'utf8')) .then(() => console.log('Done')) Mobify 33
  10. const extractDependencies = (details) => details.dependencies const isNonzeroMajorVersion = (dep)

    => !dep.version.startsWith('0') const removeMajorVersion0 = (deps) => deps.filter(isNonzeroMajorVersion) const getUrls = (deps) => _.pluck(deps, urls) const makeUrlHttps = (url) => url.replace('http:', 'https:') const makeUrlsHttps = (urls) => url.map(makeUrlHttps) const writeToFile = (filename) => (contents) => fs.writeFileAsync(filename, contents, 'utf8') Mobify 35
  11. const extractDependencies = R.prop('dependencies') const isNonzeroMajorVersion = R.not((dep) => dep.version.startsWith('0'))

    const removeMajorVersion0 = R.filter(isNonzeroMajorVersion) const getUrls = R.pluck(urls) const makeUrlHttps = R.replace('http:', 'https:') const makeUrlsHttps = R.map(makeUrlHttps) const writeToFile = R.curry(R.partialRight(fs.writeFileAsync, 'utf8')) Mobify 36
  12. Summary • Func&onal methods can help write code in an

    otherwise impera&ve environment • Code where large func&ons are built out of smaller func&ons can be cleanly self-documen&ng • Pipelines and composi&on are a powerful technique for combining func&ons in a clear way. Mobify 37