Asked  7 Months ago    Answers:  5   Viewed   35 times

I need a js sum function to work like this:

sum(1)(2) = 3
sum(1)(2)(3) = 6
sum(1)(2)(3)(4) = 10 

I heard it can't be done. But heard that if adding + in front of sum can be done. Like +sum(1)(2)(3)(4).
Any ideas of how to do this?



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;



Tuesday, June 1, 2021
answered 7 Months ago
template<class First> // 1 template parameter
void print()
    cout << 1 << endl;

#if 0
template<class First, class ... Rest> // >=1 template parameters -- ambiguity!
void print()
    cout << 1 << endl;

template<class First, class Second, class ... Rest> // >=2 template parameters
void print()
    cout << 1 << endl;
    print<Second, Rest...>();
Monday, September 20, 2021
answered 3 Months ago

For starters, I personally wouldn't recommend currying in 99% cases. It can easily make code unreadable if used improperly.

However, some of the applications that I could name would be associated with setting the function context. For example when you first execute the currying function with a context (something other than window object etc), you could have a function that applies certain calculations on the original object's properties etc when called later.

Second situation would be when, for example, you have a function that takes three arguments. The DOM element, the attribute name and it's value. This could be turned into a currying function that you could initiate with the appropriate element, then each following execution would set an attribute to the value you wish. Might be useful in cases, where you have numerous conditionals on which the attributes depend.

Friday, October 1, 2021
answered 2 Months ago

The way that the Functional library implements it is to take the parameters passed in to "curry()" as the first parameters to be passed. The function result of the "curry" operation will then take any additional parameters passed in when it is invoked and add them at the end of the argument list. It doesn't worry at all about argument list length, because that's not a fixed thing in JavaScript generally so there's really no point.


var curry = myFunction.curry("Tuesday", x + y);

So calling:

curry(100, true);

will be just like calling:

myFunction("Tuesday", x + y, 100, true);

Functional has another function called "partial()" that allows a more controlled substitution of parameters. When you call "partial()", you pass in a dummy argument ("_") to indicate where "holes" are in the argument list:

var partialFunc = myFunction.partial("Tuesday", _, 100, true, _, "banana");

Those two "_" parameters mean that the resulting "partialFunc" should drop first two arguments passed to it into those slots in the argument list:

partialFunc(x + y, "Texas");

is thus just like calling:

myFunction("Tuesday", x + y, 100, true, "Texas", "banana");

I heartily recommend getting that library and looking at the code involved. It's surprisingly succinct and clear.

One more thing: it's important to note that because JavaScript is not a lazy-evaluation language, this isn't really the same as the "curry" operation in a lazy functional language like Haskell. The distinction is that the arguments at "curry time" are evaluated and therefore sort-of "cooked" into the result. In a lazy language, things are different.

Monday, October 11, 2021
answered 2 Months ago

A function is not an std::function, it is convertible to one. You can deduce the arguments of a function, however, barring ambiguity about overloads.

#include <iostream>
#include <functional>

int foo(int bar)
  std::cout << bar << std::endl;
  return 0;

// Will cause error.
//int foo(double); 

template <typename Ret, typename... Args>
std::function<Ret (Args...)> func(Ret f(Args...))
  return f;

int main()
  auto bar = func(foo);
  bar (0); // prints 0

What you want to do with the original std::function is similar to this, which more obviously does not work:

template<typename T>
struct A

template<typename T>
void func(A<T> a);

int main()

42 is not a A, it can be converted to one, though. However, converting it to one would require T to already be known.

Saturday, October 16, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :