paint-brush
[ec6] higher order function cheatsheetby@peterchang_82818
2,354 reads
2,354 reads

[ec6] higher order function cheatsheet

by September 14th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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

Company Mentioned

Mention Thumbnail
featured image - [ec6] higher order function cheatsheet
 HackerNoon profile picture

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/