Variadic curried sum function
Not sure if I understood what you want, but function sum(n) { var v = function(x) { return sum(n + x); }; v.valueOf = v.toString = function() { return n; }; return v; } console.log(+sum(1)(2)(3)(4)); JsFiddle
Not sure if I understood what you want, but function sum(n) { var v = function(x) { return sum(n + x); }; v.valueOf = v.toString = function() { return n; }; return v; } console.log(+sum(1)(2)(3)(4)); JsFiddle
You need add to be a function that takes an argument and returns a function that takes an argument that adds the argument to add and itself. var add = function(x) { return function(y) { return x + y; }; }
Currying is mostly used if the second parameter section is a function or a by name parameter. This has two advantages. First, the function argument can then look like a code block enclosed in braces. E.g. using(new File(name)) { f => … } This reads better than the uncurried alternative: using(new File(name), f => { … Read more
1. What is currying? Currying simply means a transformation of a function of several arguments to a function of a single argument. This is most easily illustrated using an example: Take a function f that accepts three arguments: int f(int a,std::string b,float c) { // do something with a, b, and c return 0; } … Read more
(f . g) x = f (g x) This is true. You concluded from that that (f . g) x y = f (g x y) must also be true, but that is not the case. In fact, the following is true: (f . g) x y = f (g x) y which is not … Read more
You can do it by having your function f return a functor, i.e., an object that implements operator(). Here is one way to do it: struct sum { double val; sum(double a) : val(a) {} sum operator()(double a) { return val + a; } operator double() const { return val; } }; sum f(double a) … Read more
It makes you able to do e.g.: scala> def foo(as: Int*)(bs: Int*)(cs: Int*) = as.sum * bs.sum * cs.sum foo: (as: Int*)(bs: Int*)(cs: Int*)Int scala> foo(1, 2, 3)(4, 5, 6, 7, 9)(10, 11) res7: Int = 3906
The semantic difference has been explained fairly well in the answer linked to by Plasty Grove. In terms of functionality, there doesn’t seem much of a difference, though. Let’s look at some examples to verify that. First, a normal function: scala> def modN(n: Int, x: Int): Boolean = ((x % n) == 0) scala> modN(5, … Read more
Multiple Parameter List Methods For Type Inference Methods with multiple parameter sections can be used to assist local type inference, by using parameters in the first section to infer type arguments that will provide an expected type for an argument in the subsequent section. foldLeft in the standard library is the canonical example of this. … Read more
Strictly speaking, this is not a curried function, but a method with multiple argument lists, although admittedly it looks like a function. As you said, the multiple arguments lists allow the method to be used in the place of a partially applied function. (Sorry for the generally silly examples I use) object NonCurr { def … Read more