This is the 28th day of my participation in the August Text Challenge.More challenges in August
One: functional programming
The core idea of functional programming is to abstract operations into functions that can be reused anywhere and that focus only on the target when we use them. The logic is abstracted into functions.
What is functional programming
Functional Programming (FP), FP is one of the Programming paradigms, we often hear of procedural Programming, object-oriented Programming
- The way of thinking of object-oriented programming: abstract the things in the real world into classes and objects in the program world, and demonstrate the connection of things and events through encapsulation, inheritance and polymorphism
- Functional programming thinking: Abstracting things and their relationships from the real world to the program world (Abstract the operation process)
- The essence of the program: according to the input through some operation to obtain the corresponding output, the program development process will involve many functions with input and output
- X -> f ->y, y=f(x)
- A function in functional programming is not a function (method) in a program, but a function (mapping) in mathematics, for example: y=sin(x), the relationship between x and y
- The same input always produces the same output (pure function)
- Functional programming is used to describe mappings between data (functions)
Functional Programming Concept 1: What is a pure function
A pure function is a function that has no side effects
- Does not change the value of the function argument
- External scoped variables are not called (safe and reliable, function and external decoupling)
- The same input always produces the same output
Pure functions do not need access to shared memory data, so they can run at will in parallel (Web workers)
Functional programming concept 2: Curritization
Any basic function with multiple arguments can be designated as a “custom function” by assigning the first few arguments to the function using techniques such as closures.
A large number of custom functions can be obtained through the Currization of the base function.
When a function has three arguments it is called a ternary function
A function of several variables can be converted into a function of one variable by Corritization, which facilitates the combination of functions.
Functional programming maximizes code reuse.
The problem of hard coding is solved by Currization, which allows functions with very low granularity to combine with each other. Form functions that have many functions.
Functions are first-class citizens
- Functions can be stored in variables
- Functions can be arguments
- Functions can be made more flexible by masking their internal implementations. Use the most flexible functions exposed as interfaces.
- The implementation of a function is user-defined
- The callback
- Functions can be made more flexible by masking their internal implementations. Use the most flexible functions exposed as interfaces.
- A function can be a return value
In JS, a Function is just an ordinary object (via new Function()), we can store the Function in a variable/array, it can also be used as an argument and return value of another Function, We can even construct a new Function while the program is running via new Function(‘alert(1)’)
- Assigning functions to variables (functions can be stored in variables)
What is a higher-order function (one that satisfies at least one of the following conditions)
In general, in programming, it is common to pass function A as an argument to function B. Function B is called A higher-order function, which is A highly abstract programming paradigm. In functional programming, callback functions are called lambda expressions. Higher-order functions have the added ability to receive and return other functions, just like normal functions. Parameters and output.
- Accepts one or more functions as output
- Output a function
The meaning of using higher-order functions
- Abstraction helps us mask the details and focus only on our goals
- Higher-order functions are used to abstract general problems.
case
-
Caching is implemented using higher-order functions
function getAreaWithMemoize (r) { console.log(r) return Math.PI * r * r } // // uses object data cached in closures // const memoizeArea = _.memoize(getAreaWithMemoize) // console.log(memoizeArea(5)) // console.log(memoizeArea(5)) // console.log(memoizeArea(5)) function _memoize (fn) { let cache = {} return function () { // The same parameters are directly used in the cache let key = JSON.stringify(arguments) cache[key] = cache[key] || fn.apply(this.arguments) return cache[key] } } const myMemoizeArea = _memoize(getAreaWithMemoize) console.log(myMemoizeArea(5)) console.log(myMemoizeArea(5)) console.log(myMemoizeArea(5)) Copy the code
-
Implement currization in Lodash
const _ = require('lodash') function getSum (a, b, c) { return a + b + c } // const curry = _.curry(getSum) // console.log(curry(1, 2, 3)) // console.log(curry(1, 2)(3)) // console.log(curry(1)(2, 3)) // Implement currization in Lodash /* In a return function, check whether the format of the argument passed is smaller than the required parameter. If not, call the function directly. If so, return a function that receives the next parameter. The recursive function then merges the passed argument with the one that has already been passed so that the arguments can be merged. The comparison continues within the function with fn's length, continues to recursively merge arguments if less, and calls FN (function) with merged arguments(arguments) if greater. * / function cur (fn) { return function curryFn () { const args = Array.prototype.slice.call(arguments) console.log(args) // The length referenced by the function can get the number of parameters of the function // console.log(args.length, fn.length) if (args.length < fn.length) { return function () { returncurryFn(... args.concat(Array.from(arguments)))}}else { return fn.apply(this.arguments)}}}const curry = cur(getSum) console.log(curry(1.2.3)) console.log(curry(1.2) (3)) console.log(curry(1) (2.3)) console.log(curry(1) (2) (3)) Copy the code