Function currying (Currying) small practice

What is the function currying

In computer science, curried (Currying) is the function of receiving a plurality of parameters into a single parameter receiving function (the first function of the first parameter), and accepts the new function returns the remaining arguments and returns the result of technology. This technology consists Christopher Strachey logician Haskell Curry named, although it is Moses Schnfinkel and Gottlob Frege invention.
For example, suppose that the programmer is henpecked A, all the wages to his wife, in order to buy a favorite keyboard
u=3317326080,3565966017&fm=27&gp=0.jpg

A programmers are secretly hiding a few cents a day, going to the end to buy a keyboard, so programmers wrote the following A statistical method is used to this end to scrape together a total of how much money, as follows:

/**
 * @description 统计金额
 * @return {number}
 */
function countMoney() {
  let money = 0
  // 温馨提示:arguments是所接收的所有参数组成的类数组,不懂的需要搜一搜补补知识啦
  for (let i = 0; i < arguments.length; i++) {
    money += arguments[i]
  }

  return money
}
// 藏了一年的账本记录的数据
const records = [1, 1, 2, 2, 3, 3, 4, 4]
// 把全部数据都输入进行计算
countMoney(1, 1, 2, 2, 3, 3, 4, 4)

The method presented in the above code, is the most direct method of calculation, which is inconvenient places that A programmer should take a small notebook to keep a record first day how much money! ! ! This book was found in case it ultimately kneeling keyboard, and extreme insecurity.
u=864471950,2685274187&fm=27&gp=0.jpg

Possession of their own money more safely

In fact, under the current data recorded daily it is not flexible, and function currying the effective solution to this problem.
We want to do our own money storage

// 2018-01-01 存了1毛钱
countMoney(1)
// 2018-01-02 存了2毛钱
countMoney(2)
// 2018-01-03 存了3毛钱
countMoney(3)
// 2018-01-04 存了4毛钱
countMoney(4)
//一年以后
// 统计这笔巨额存款
countMoney()

Above this approach, we do not care to store the recorded data, we only need a day to piggy bank stuffed the money, and then taken out at the end is a sum. Then do not worry about leaving the small books as evidence!

Implementing a function code currying

/**
 * @description countMoney为立即执行函数,返回的结果是另一个函数
 */
const countMoney = (function () {
  let money = 0
  let args = []
  const res = function () {
    if (arguments.length === 0) {
      for (let i = 0; i < args.length; i++) {
        money += args[i]
      }
      return money
    } else {
      // arguments 是个类数组来着,应该用展开符展开才能push进去
      args.push(...arguments)
      return res
    }
  }
  return res
})()

// 2018-01-01 存了1毛钱
countMoney(1)
// 2018-01-02 存了2毛钱
countMoney(2)
// 2018-01-03 存了3毛钱
countMoney(3)
// 2018-01-04 存了4毛钱
countMoney(4)
//一年以后
// 统计这笔巨额存款 输出结果为 10
console.log(countMoney())
// 你还可以装逼地进行花式统计,结果同样是10
countMoney(1)(2)(3)(4)()

Analysis Code

In fact, a lot of thought and design patterns in JavaScript, the closure is a very common and very important thing, the above code, in essence, is the use of closures.
This function is executed immediately function that returns a new function, and this function is actually a new closure, this new function to each received parameters are stored,
and continue to return a new function, when a call to find when no incoming parameters, it would mean in terms of statistics, so that the stored data before one-off to get
out of the calculation, and returns the results. Process is as follows:
5b6d0ab0e4b053a09c2e0a95.png

to sum up

The so-called curried functions, or some other concepts are also involved in development, such as closures, singleton, observer mode, etc. are good, the point is that we need to focus on master
code design of these models or concepts thought, in order to better serve our business development, so that our code more robust, flexible and efficient.

Guess you like

Origin www.cnblogs.com/jlfw/p/11871260.html