6 basic terminology JavaScript function to keep in mind!

You may be very skilled, but not necessarily know the name.

Starting this text ~

image

Let's talk about what is: lambdas (anonymous function), first-class functions (first-class function), higher-order functions (higher-order functions), unary functions (functions of one variable), currying (curried) and pure functions (pure function).

If you do not know the difference between imperative and declarative programming, you can read my article: Imperative versus declarative code ... what's the difference?

What is Lambdas (anonymous) => arrow function?

Lambdas (λ) in the JavaScript as the arrow functions (arrow function) is widely known:

// this is your regular named function in JavaScript
function namedFunction (a, b) {
 return a + b;
}
// this is a lambda, i.e. an arrow function
const lambda = (a, b) => a + b;

The term lambda is a formal mathematical logic system, originated in the lambda calculus. Lambda calculus is Turing complete, it represents a general purpose computing model can be constructed of any Turing machine. Lambda expressions (anonymous function expression) is the cornerstone of functional programming. If it helps, just shorten it as a new grammar function on the line. Note, however, this point of use thereof in a subject or class.

image

What is first-class functions (first-class function)?

First-class type means that the value of this type can be used as a variable. In JavaScript, a string is a first class type, a type of function is also first class. Therefore, the function can accept other functions as arguments and return functions as a return value.

When binding event listener function is used as a first-class:

const handler = () => console.log ('I am function');
document.addEventListener ('click', handler);

What is the higher-order functions (higher-order functions)?

Other higher-order function is a function accepts as a parameter or a function as a function of the value returned. First-order function (a step function) is a function, it does not accept as a function of other parameters, and does not return as a function of its return value.

const firstOrder = () => console.log ('First order strikes back!');
const higherOrder = whoStrikesBack => whoStrikesBack ();
higherOrder (firstOrder);

What is unary functions (functions of one variable)?

The term refers to a function that takes some of the metadata parameters. Unary functions (ie monadic) is a function that accepts only one parameter.

const unaryFunction = message => console.log (message);
const binaryFunction = (color, message) =>
 console.log (`%c${message}`, `color:${color}`);
const ternaryFunction = (fnc, color, message) =>
 fnc (`%c${message}`, `color:${color}`);

What is currying (curried)?

Currying (currying) is a function of a number of parameters and with a function for converting a sequence of process, only one parameter for each function.

A function with n arguments, it can be used curried into a unary function.

const binaryFunction = (a, b) => a + b;
const curryUnaryFunction = a => b => a + b;
curryUnaryFunction (1); // returns a function: b => 1 + b
curryUnaryFunction (1) (2); // returns the number 3

Currying (currying) to mathematician Haskell Curry named, not to eat.

Currying function is very suitable for improving the reusability of code structure and function. To learn more, please refer to: JavaScript ES6 curry functions with practical examples. It might make people accustomed to, but I write all the functions attributed to currying.

image

What is pure functions (pure function)?

It is a pure function return value is determined only by the parameters, without any side effects of functions.

This means that if you put a different one hundred calls to the same parameters in a pure function throughout the application of a hundred times, the function always returns the same value. Pure function does not change or read outside the state.

let myArray = [];
const impureAddNumber = number => myArray.push (number);
const pureAddNumber = number => anArray =>
 anArray.concat ([number]);
console.log (impureAddNumber (2)); // returns 1
console.log (myArray); // returns [2]
console.log (pureAddNumber (3) (myArray)); // returns [2, 3]
console.log (myArray); // returns [2]
myArray = pureAddNumber (3) (myArray);
console.log (myArray); // returns [2, 3]

In the array, Push function is impure, it will change its array of calls, and thus may produce side effects. push return value is a numeric index.

Further, Concat receiving array and connected to the other array, thereby generating a new array without side effects. And then return after new array of two arrays in series.

Pure functions are important because they simplify unit testing (no side effects and does not require dependency injection), they avoid tight coupling, and by eliminating the side effect of making the application more concise.

Epilogue

Understanding of functional programming does not make you a better developer, it makes you a better person. You can entertain your friends with beer by the lambda calculus, can be corrected by your family interesting mathematical logic.

image

Finally, I recommend a push within the circle of exchange learning advanced front-end ( front-end information-sharing ), no matter which direction you in the earth,
whether you are welcome to work in a few years you settled in! (Regularly offers a number of free books collection of free learning materials and tidied face questions and answers document!)

If you have any objection to this article, then please write your comments in the article comments at.

If you find this article interesting, please share and forward, or you can look at, you acknowledge and encouragement of our articles.

Wish everyone in the programming this road, farther and farther.

Guess you like

Origin blog.csdn.net/ITFENGHUOLUN/article/details/90699941