Learn about the JavaScript functional programming directory

  • 0- Understand JavaScript functional programming – What are pure functions
  • 1- Understand JavaScript functional programming – Currification
  • 2- Understand the benefits of JavaScript functional programming-code combinations
  • 3- Understand JavaScript functional programming – declarative functions
  • 4- Understand JavaScript functional programming – type signing

Coding principle

  • DRY (Don’t repeat yourself, Don’t repeat Yourself)

  • Loose coupling high Cohesion

  • YAGNI (Ya ain’t gonna need it)

  • The Principle of Least Surprise

  • Single responsibility and so on.

Let’s do an example of a pure function

  • Pure functions do not change the original input value. Avoid unwanted side effects
var xs = [1.2.3.4.5];

/ / pure
xs.slice(0.3);
/ / = > [1, 2, 3]

xs.slice(0.3);
/ / = > [1, 2, 3]

xs.slice(0.3);
/ / = > [1, 2, 3]


/ / not pure
xs.splice(0.3);
/ / = > [1, 2, 3]

xs.splice(0.3);
/ / = > [4, 5)

xs.splice(0.3);
/ / = > []
Copy the code

Silce is a pure function that doesn’t change its initial value. The common splice is an impure function that affects the initial value.

Side effects

Side effects may include, but are not limited to:

Change the file system to insert records into the database send an HTTP request variable data print /log to get user input DOM query access system statusCopy the code

We can generalize that any interaction with the external environment is a side effect. We try to keep side-effect free interactions when writing pure functions.

Of course, this is not to ban all side effects, but we need to control the scope of occurrence.

Pursuit of pure function

When we started to learn function in junior high school, we knew that function is a special relationship between different values: each input value returns and only one output value.

  • We want to keep this logic and let the function get a certain value.

Cacheable

Let’s implement a cached function

var squareNumber  = memoize(function(x){ return x*x; });

squareNumber(4);
/ / = > 16

squareNumber(4); // Read the result of input value 4 from the cache
/ / = > 16

squareNumber(5);
/ / = > 25

squareNumber(5); // Read the result with input value 5 from the cache
/ / = > 25
Copy the code

Here’s how the Memoize cache function is implemented. Okay

var memoize = function(f) {
  var cache = {};

  return function() {
    var arg_str = JSON.stringify(arguments);
    cache[arg_str] = cache[arg_str] || f.apply(f, arguments);
    return cache[arg_str];
  };
};
Copy the code

See the utility of this caching function below

Portability/self-documentation

A pure function is completely self-contained; everything it needs is readily available. Think about it… What are the benefits of this self-sufficiency? First, the dependence of pure functions is explicit and therefore easier to observe and understand

Parallel code

Finally, and crucially, we can run any pure function in parallel. Because pure functions do not need to access shared memory at all, and by definition, pure functions do not enter a race condition due to side effects.

Parallel code is easy to implement in server-side JS environments and in browsers that use Web workers because they use threads. However, due to the complexity of impure functions, the current mainstream view avoids such parallelism.

conclusion

Apply the above rules to the proper use of pure functional programming, so that our code will be more elegant.