Topic request

This time we want to write calculations using functions and get the results. Let’s have a look at some examples:

seven(times(five())); // must return 35

four(plus(nine())); // must return 13

eight(minus(three())); // must return 5

six(dividedBy(two())); // must return 3

Requirements:

There must be a function for each number from 0 (“zero”) to 9 (“nine”) There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby and Python) Each calculation consist of exactly one operation and two numbers The most outer function represents the left operand, the most inner function represents the right operand Division should be integer division. For example, This should return 2, not 2.666666… :

eight(dividedBy(three()));  
Copy the code

Their thinking

According to the question, it is required to add and subtract up to 10 by running a function. For example, perform 3 x 5. Then the corresponding calling function is

three(times(five()))
Copy the code

The run result should be 15.

There are two things we can learn from our initial observations.

  • Numerical functions (here we call functions such as three() numerical functions) may or may not take arguments.
  • Operant functions (here we call plus(), times(), etc.) must have arguments.

In the example above, three(times(five()))five has no argument passed. Three has an argument, and the argument is the result of the operation function times. The Times function has arguments that are the result of the operation of the five function. So we can roughly write our function as follows:

// A numeric function
function three(fn) {
    if(fn ! =undefined) {
        //code...
    }
    else {
        //code...}}// Operate the function
function times(fn) {
    //code...
    return //something...
}
Copy the code

Take a closer look at the execution of the functions given in the question, in the following order:

  1. The five function is executed and the result of the five function is passed as an argument to The Times function.
  2. The Times function takes the result of the execution of five as an argument, and gives the result of the execution as an argument to three after the execution is complete.
  3. The three function runs with the result of The Times function as an argument, and finally outputs the result.

As you can see, functions are executed from the inside out, and the inner functions return the results of their execution to the outer functions. Next, analyze the problem from the inside out:

So let’s first look at the function five, what should it do? Here, it doesn’t seem to need to do much work; it simply returns the value of the current function (three for 3, for example) to The Times function. So we can get the number function, when the function has no arguments. Simply return the corresponding value. As follows:

function five(fn) {
    if(fn ! =undefined) {
        //code...
    }
    else {
        return 5; }}Copy the code

And then next, we go to The Times function, and what does The Times function do here? Normally, four operations would be performed, but four operations require two operands. In the previous step, we gave the result of the five function, 5, as an argument to The Times function. It should look like this:

function times(5){
    //code...
    return //something...
}
Copy the code

So we only have one side of the operand, and the other operand is in the outer layer. We can’t call it, so how do we get both operands in order for all four operations to work?

That’s right! Closure, by definition, if we declare a function in times, multiply it, and the right-hand operand is five, that’s the argument. Then the function is returned. When the function returns, it will enter the outermost function three. At this time, because the return value of times is a function, and this function is passed to three as a parameter, so it is in the function three. We pass the corresponding value of the three function as an argument to the function returned by The Times function, and then execute the function, so that we can get the result we want.

So we can write a function of times as follows:

function times(fn){
    // where y is used to pass arguments to outer functions
    return function(y){y*fn}
}
Copy the code

And the function of three becomes clear. Simply pass the value to times and return the result of the function’s execution. The code is as follows:

function three(fn){
    if(fn ! =undefined) return fn(3)
    else / /...
}
Copy the code

By combining the three function with the five function, we get a general formula for the number function

Num = 3; numName = 3; numName = 3
fnction numName(fn){ fn ! =undefined? fn(num):num }Copy the code

And the operation function looks like this:

If opName = plus, then the following formula is y + fn
function opName(fn){
    return function(y){y op fn}
}
Copy the code

At this point, the problem is basically solved. ParseInt = parseInt; parseInt = parseInt; parseInt = parseInt; parseInt = parseInt