This is the 27th day of my participation in Gwen Challenge

A programming style based on procedural abstraction. In functional programming, a function is a pipe. One value goes in here, a new value comes out there, nothing else

In functional programming, functions are treated as first-class citizens. JavaScript can build abstractions by cleverly combining functions. By embedding functions, in software development, we care more about “what does a function do” than “how does a function do it?

1. Functions must be pure without side effects. A function is pure if its input parameters are deterministic and its output is uniquely deterministic.

function setcolor(el,color){
    el.style.color=color;
}
 function setcolors(els,color){
    Array.from(els).map(el= > setcolor(el,color));
 }
setcolors(document.querySelectorAll('#lu li'),'red');
Copy the code

The setcolor() function operates on the DOM, which is not a pure function. Setcolors () does not operate on the DOM directly, but calls setcolor inside the function, so it is not a pure function. Higher order function process abstraction improves function purity

function setcolor(el,color){
    el.style.color=color;
}

function __multi__(fn){
    return function(arrayLink,... args){
        return Array.from(arrayLink).map(item= > fn(item,...args));
    }
}

var setcolors=__multi__(setcolor);
setcolors(document.querySelectorAll('#lu li'),'red');
Copy the code

The multi higher-order function actually makes a change to fn. When the first operand passed in is an arrayLink, the fn operation is performed on each element of the arrayLink, simplifying the operation process

The wrapped multi function is used for batch processing and has many uses, such as:

function __multi__(fn){
    return function(arrayLink,... args){
        return Array.from(arrayLink).map(item= > fn(item,...args));
    }
}
function add(x,y){
    return x+y;
}
var m_add=__multi__(add);
console.log(m_add([1.2.3].3));   / / (4 and 6)
Copy the code

The m_add() function does not take into account the parameters passed in. The multi function converts the parameters.

Criterion function

function reduce(fn,... args){
    return args.reduce(fn);
}
function add(x,y){
    return x+y;
}
console.log(reduce(add,1.2.3));
Copy the code

The reduce function converts an array to a single value by repeatedly calling a function

2. Currization of functions

(Common knowledge: Currying, also known as carryization or callization, is a technique of converting a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returning a new function that takes the remaining arguments and returns a result.)

var add=(x,y) = > x+y;

// Currization of simple addition:
var add=function(x){
    return function(y){
        returnx+y; }}Copy the code

In fact, currification is a way of “preloading” a function by passing in fewer arguments and getting a new function that already remembers those arguments. In a sense, this is a “caching” of arguments. Basically, you take a function with many parameters, and you turn it into a single parameter function

3. Function combination

So once you have pure functions and you have Currie, you can do combinations of functions

// function composition
var compose=function(f,g){
    return function(x){
        returnf(g(x)); }}/ / curry
var add=function(x){
    return function(y){
        return Math.pow(x,2) +Math.pow(y,2); }}var sqrt=function(a){
    return Math.sqrt(a);
}
console.log(sqrt(add(3) (4)));
Copy the code

Using Pythagorean theorem to simply explain the function of synthesis, although write complex, but can clearly explain the function of synthesis and the principle of Currie.

Advantages of functional programming

Unit testing: The result of a function evaluation is only its return value, and the only thing that affects its return value is the function’s parameters. For each function in the program under test, you care only about its parameters, regardless of the order in which the function is called, and you don’t have to carefully set the external state. All you have to do is pass the argument parallelism that represents the marginal case: no data in a functional program is modified twice by the same thread, let alone two different threads, and without worrying about deadlocks and critical sections, functional programs can execute in parallel without modification