javascriptunderscore.js

Lazy range iteration using underscore


I've caught myself using this in place of a traditional for loop:

_.each(_.range(count), function(i){
  ...
});

The disadvantage being creating an unnecessary array of size count.

Still, i prefer the semantics of, for example, .each(.range(10,0,-1), ...); when iterating backwards.

Is there any way to do a lazy iteration over range, as with pythons xrange?


Solution

  • Considering the source of underscore.js says the following about range:

    Generate an integer Array containing an arithmetic progression

    I doubt there is a way to do lazy iteration without modifying the source.