1. Higher-Order Functions (HOF)

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. – C.A.R. Hoare, 1980 ACM Turing Award…


This content originally appeared on DEV Community and was authored by Ajay kumbhare

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. - C.A.R. Hoare, 1980 ACM Turing Award Lecture

A higher-order function is a function that takes a function as an argument or returns a function.

When we were in school we learned algebra formulas like

f.g = f(g(x))

can be translated into JavaScript

const compose = (f, g) => x => f(g(x));

How to write HOF?

const filter = (predicate, xs) => xs.filter(predicate);

const isEven = (type) => (x) => Object(x) instanceof type && x % 2 === 0;

filter(isEven(Number), [2, "1", 4, null, undefined, 100, "6"]);
// [2, 4, 100]

If you see the above code I have created 2 functions filter and isEven. filter function accepts two arguments function and array so we can say that filter function is higher-order function.

So, the predicate is -> isEven(Number) so both are functions (isEven and Number)

xs.filter(predicate)

Equivalent to

xs.filter(isEven(Number))

If you see the definition of isEven function its curry function so you can call curry function like

function_name(argument_1)(argument_2)

So, when xs.filter(predicate) executes its passing array values too in a predicate function like

xs.filter(val=>predicate(val))
// or
xs.filter(val=>isEven(Number)(val)) 

So, when you pass function and array in the filter function it will filter values based on your predicate function and array values.

I am learning FP. If you feel I am wrong then please feel free to write a comment in the comment box so I will update the document.


This content originally appeared on DEV Community and was authored by Ajay kumbhare


Print Share Comment Cite Upload Translate Updates
APA

Ajay kumbhare | Sciencx (2021-03-11T08:30:15+00:00) 1. Higher-Order Functions (HOF). Retrieved from https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/

MLA
" » 1. Higher-Order Functions (HOF)." Ajay kumbhare | Sciencx - Thursday March 11, 2021, https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/
HARVARD
Ajay kumbhare | Sciencx Thursday March 11, 2021 » 1. Higher-Order Functions (HOF)., viewed ,<https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/>
VANCOUVER
Ajay kumbhare | Sciencx - » 1. Higher-Order Functions (HOF). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/
CHICAGO
" » 1. Higher-Order Functions (HOF)." Ajay kumbhare | Sciencx - Accessed . https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/
IEEE
" » 1. Higher-Order Functions (HOF)." Ajay kumbhare | Sciencx [Online]. Available: https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/. [Accessed: ]
rf:citation
» 1. Higher-Order Functions (HOF) | Ajay kumbhare | Sciencx | https://www.scien.cx/2021/03/11/1-higher-order-functions-hof/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.