- 对于 Object 的遍历;
Details
Function composition is a powerful technique. For example:
function sum(x, y) {
return x + y;
}
function double(x) {
return sum(x, x);
}
function minus (x, y) {
return x - y;
}
function addOne(x) {
return sum(x, 1);
}
double(sum(2, 3)); // 10
But in complex expressions, composition may be difficult to understand. For example:
double(double(addOne(sum(7, minus(sum(5, sum(4, 5)), 4))))); // 72
In this kata, we will implement a function that allows us to perform this by applying a fluid style:
c.sum(4, 5).sum(5).minus(4).sum(7).addOne().double().double().execute(); // 72
Your job is implement the chain
function:
function chain(fns) {
}
var c = chain({sum: sum, minus: minus, double: double, addOne: addOne});
As you can see, this function receives the methods to be chained and returns an object that allows you to call the chained methods. The result is obtained by calling the execute
method.
Chained functions receive an arbitrary number of arguments. The first function in the chain receives all its arguments. In the other functions, the first argument is the result of the previous function and then it only receives the remainder arguments (second, third, etc.). The tests always pass the appropriate arguments and you do not have to worry about checking this.
Note that the chain can be reused (the internal state is not stored):