0

I am just wondering what is the reason for so abundant for loop in the Array initialization. For example this code:

count = (0 for [0..@size])

gives:

 return count = (function() {
   var _i, _ref, _results;
  _results = [];
    for (_i = 0, _ref = this.size; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--) {
      _results.push(0);
    }
    return _results;
  }).call(this);

Why Coffeescript uses so redundant code instead of:

for (var i = 0; i < this.size; i++) {
   _results[i] = 0;
}

1 Answer 1

1

It might look a redundant in single usage of that but generally it built in way to consider all possible scenarios and make generated code safer as it possible. I can see at least three issues solved by that code (not sure how correctly call these):

  1. Variable name conflict
  2. Variable scope conflict
  3. Backward loop support (when size < 0)

You can read a bit there.

Sign up to request clarification or add additional context in comments.

Comments

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.