1

Can anyone see a problem with a recursive function written like this:

var recurse = 100;
var recursed = 0;

(function (callback){

  callback(callback);

})(function(callback){

  recursed++;

  console.log('recursed ' + recursed + ' times');

  if(recursed < recurse){

    callback(callback);

  }
});

Is there any reason why this might execute slower? Or might be considered bad practise?

Is this functional programming gone AWOL?

5
  • Is this supposed to by the Y combinator? Why do you use it in a language that supports recursion natively? Btw, a simple loop would've been the better choice - or what did you want to demonstrate with this example? Commented Jan 20, 2014 at 0:10
  • I suppose I just prefer it visually for executing recursive functions within functions if I know the code won't be re-called. But I wanted to know if anyone thought that it might be more programmatically expensive. I appreciate that I don't need to trick JavaScript into doing something that it already does. Commented Jan 20, 2014 at 0:18
  • This isn't a Y combinator. It still hits a call stack limit. Commented Jan 20, 2014 at 0:21
  • @cookiemonster: The Y combinator was never supposed to get around stack limits? But you're right, this is not a Js Y combinator Commented Jan 20, 2014 at 0:25
  • @Bergi: You're right. I was mixed up. Commented Jan 20, 2014 at 0:35

2 Answers 2

4

The core principles of functional programming are data immutability and functions being functions in mathematical sense (i.e. take value and return value without any side-effects). Those principles form referential transparency, i.e. given the same arguments a function will always return the same result.

Your function does not satisfy any of those criteria to be referred to as "functional".

The following is an example of how recursion is done in functional style:

var recursiveFunction = function( count, max ) {
  if( count < max ){
    return recursiveFunction( count + 1, max )
  } else {
    return count
  }
}

var result = recursiveFunction(0, 100) // result == 99
Sign up to request clarification or add additional context in comments.

Comments

2

Is there any reason why this might execute slower?

Yes. JavaScript is hardly tail-call-optimized. A loop would be faster.

Or might be considered bad practise?

Yes. Your example code is unreadable, it's hard to grasp what all these callbacks do.

If you want to use recursion for something like this, an IENFE (Named & Immediately Executed Function Expression) would be a better choice:

(function callback(recursed) {
     console.log('recursed ' + recursed + ' times');
     if (recursed < 100)
         callback(recursed + 1);
})(0);

4 Comments

Cheers. Your example is clearly much better. You may want to lose the semi-colon from the end of the if statement otherwise it probably won't validate the limit.
Ooops, how did that get there? Thanks for the cue :-)
Just to confirm, when you say A loop would be faster, you're referring to a for-loop? I'm traversing DOM trees, so for-loop won't suffice. The simple if(recursed < recurse) was just to demonstrate the element recursing as opposed to suggesting that it will only be recursing in one direction.
Yes, I meant a for-loop. Of course, for traversing trees recursion is almost necessary to make for readable code :-) I actually use that IENFE pattern quite often for DOM traversal, though some people like a normal function declaration plus an explict call better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.