Don't say so much, just coding...
We want to create a function that will add numbers together when called in succession.
add(1)(2);
// returns 3
We also want to be able to continue to add numbers to our chain.
add(1)(2)(3); // 6
add(1)(2)(3)(4); // 10
add(1)(2)(3)(4)(5); // 15
and so on.
A single call should return the number passed in.
add(1); // 1
We should be able to store the returned values and reuse them.
var addTwo = add(2);
addTwo; // 2
addTwo + 5; // 7
addTwo(3); // 5
addTwo(3)(5); // 10
We can assume any number being passed in will be valid whole number.
def add(n)
# ...
end
Test.expect(add(1) == 1);
Test.expect(add(1).(2) == 3);
Test.expect(add(1).(2).(3) == 6);
function add(n){
// Let the currying begin!
}
Test.expect(add(1) == 1);
Test.expect(add(1)(2) == 3);
Test.expect(add(1)(2)(3) == 6);
# Solution 1
def add(n)
n
end
class Fixnum
def call(m)
self + m
end
end
# Solution 2
def add(n)
def call(m)
return self + m
end
return n
end
// Solution 1
function add(n){
var fn = function(x) {
return add(n + x);
};
fn.valueOf = function() {
return n;
};
return fn;
}
// Solution2
function add(n) {
const f = x => add(n + x)
f.valueOf = () => n
return f;
}
>>: Day30-1 - GitLab CI 可以怎麽重构及整理 .gitlab-ci.yml 让内容更好了解?
上一篇讲到离职时整理档案的重要,相对的每个人的工作日志或工作排程,也是个人的保命符,一定要写得清楚明...
前言 今天来聊一个看起来不浪费的浪费。 多工会怎样 在我们的成长过程中,应该不只一次会听到前辈们的告...
一页式网站 SPA SPA 全名 Single Page Applications 只有一个 HTM...
写铁人赛是一个非常有趣的过程,也让我在工作之余会继续接触程序,这次参赛的目的也算是整理自己在工作时需...
Virtual Judge ZeroJudge 题意 输入一整数,判断是否为 11 的倍数 需要注...