3

Let me propose an example that works, then follow up with what fails, highlighting the point to my question.

Here, we have 3 functions being called (1 named, 2 anonymous):

var add = function(a, b) {return a+b};
var multiply = function(a, b) {return a*b};

function myFunction(fxn) {
    return function(x) {
        return function(y) {
            return fxn(x,y);
        }
    }
}

myFunction(add)(2)(3)

Understandably, this call fails:

myFunction(add)(2)(3)(4)

How would I detect how many functions are being called? In the 2nd call, I'm calling 4 functions (1 named, 3 anonymous).

How would I rewrite the myFunction function in a way that compensated for any given amount of calls? I know we can detect how many arguments a function was given, but is there a way to detect how many functions are being called? I hope I worded this correctly. Thanks.

The Qodesmith
  • 3,205
  • 4
  • 32
  • 45
  • The problem is, ultimately the innermost function in `myFunction` returns the result of another function - at that point it's out of the scope of `myFunction`, and there's no way for it to know if the result is going to be used as intended, or as in your example attempt to be executed as a function itself. `var x = myFunction(add)(2)(3); x(4);` would have the same effect - and `myFunction` wouldn't know anything about it. – James Thorpe Jun 15 '15 at 21:40
  • So are you saying you want to add any series of numbers by invoking the function an additional time? So, for example, you want `myFunction(add)(1)(2)(3)(4)` to return `9`? – Aweary Jun 15 '15 at 21:40
  • 2
    You're looking to implement [currying](http://tech.pro/tutorial/2011/functional-javascript-part-4-function-currying). – Austin Brunkhorst Jun 15 '15 at 21:42
  • 1
    What are you trying to achive? – Henri Podolski Jun 15 '15 at 21:42
  • @Aweary I dont think thats what hes saying. I think he wants to know how many functions have been called, he wants to keep track of the number of functions. – Craicerjack Jun 15 '15 at 21:43
  • 1
    Seems like that's exactly what he means by "How would I rewrite the myFunction function in a way that compensated for any given amount of calls". If you want to see how many functions are invoked, you'd have to add some kind of counter functionality that incremented some global or parent value each time the function was called. This seems all rather silly though, in terms of practicality. – Aweary Jun 15 '15 at 21:44
  • @Aweary - Yes, that's what I'd like to know how to do. Any by the way, that would add up to 10, not 9 ;) And I agree, it might be silly in terms of practicality, but this is all fashioned to help me learn JavaScript deeper. – The Qodesmith Jun 15 '15 at 21:59
  • @HenriPodolski - this whole scenario was really me answering a small coding challenge from a friend. Then I just starting thinking, how would I account for more calls then are currently allowed? – The Qodesmith Jun 15 '15 at 22:02
  • @QuestTheWordsmith and the JS interpreter did a good job, by throwing no method errors to you, I guess? But your description includes another possible intention: Not just how to count the number of nested callbacks, but how to rewrite your method to accept any number of parentheses. Correct? – Henri Podolski Jun 15 '15 at 22:29
  • The only solution is to *always* return a function. Which means you cannot really return a result. – Bergi Jun 15 '15 at 22:48
  • @HenriPodolski I was using repl.it and it did throw an error. Anyway, yes, how to rewrite the method to accept any number of parentheses. – The Qodesmith Jun 15 '15 at 22:51
  • possible duplicate of http://stackoverflow.com/a/18067040/1048572 or http://stackoverflow.com/questions/5832891/javascript-sum-function (and many more) – Bergi Jun 15 '15 at 23:00

2 Answers2

0

To find out if a variable contains a reference to a function you can use below code:

if (typeof(v) === "function") alert("This is a function")

Based on above you can find out on how many nested functions there are

function myFunction() {
  return function() {
    return function() {
      return 1 + 2;
    }
  }
}

var count = 0;
var v = myFunction();

while (typeof(v) === "function") {
  count++;
  v = v();
}
alert("Nr of nested functions: " + count)
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
0

Even if this has no practical use case I can think of, this is a possible solution:

var add = function(a, b) {
  return a + b
};
var multiply = function(a, b) {
  return a * b
};
var counter = 0;
var result = 0;

function myFunction(fxn) {
  counter = 1;
  result = 0;
  return function first(x) {
    ++counter;
    return function second(y) {
      ++counter;
      x = result ? result : x;
      result = fxn(x, y);
      return second;
    }
  }
}

myFunction(add)(1)(2)(3)(4);
alert('Result is: ' + result + '; Parentheses count: ' + counter);