[ec6] higher order function cheatsheet

Written by peterchang_82818 | Published 2017/09/14
Tech Story Tags: javascript | nodejs | react | higher-order-function

TLDRvia the TL;DR App

Step 0

Understanding the Classic

To illustrate writing higher order functions with arrows, let’s look at a classic example: add. In ES5 that would look like this:

function add(x){  return function(y){    return y + x;  };}var addTwo = add(2);addTwo(3);          // => 5add(10)(11);        // => 21

Use Case 1 — C**ompound Function**

Transformation (From IBM)

It is a simple example of rewriting classic compound function into HOF

const add = x => y => y + x;var addTwo = add(2);addTwo(3);          // => 5add(10)(11);        // => 21

Use Case 2 — Iteration Function

Short circuit Iteration Function, such as sort, forEach, filter, some

**const isNumber = n => x => x === n**const arr = ['1','2','3']

arr.filter( isNumber("3") )

Why is this useful?

This is useful for a few reasons:

  • It reduces repetitive code
  • It allows for easier reuse of code
  • It increases clarity of code meaning

You may also like

_— [ES6] Functional Programming: cheatsheet:_https://medium.com/@peterchang_82818/es6-function-programming-cheatsheet-update-spread-note-example-tutorial-26f265b0ddf1

Credit:

— https://developer.ibm.com/node/2016/01/11/higher-order-functions-in-es6easy-as-a-b-c/


Published by HackerNoon on 2017/09/14