LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Superfluous closure for returned list comprehension

Open summivox opened this issue 9 years ago • 1 comments

f = (a) ->
  x = [.. for a]
  y = [.. for a]

currently compiles to:

f = function(a){
  var x, res$, i$, x$, len$, y;
  res$ = [];
  for (i$ = 0, len$ = a.length; i$ < len$; ++i$) {
    x$ = a[i$];
    res$.push(x$);
  }
  x = res$;
  return y = (function(){
    var i$, x$, ref$, len$, results$ = [];
    for (i$ = 0, len$ = (ref$ = a).length; i$ < len$; ++i$) {
      x$ = ref$[i$];
      results$.push(x$);
    }
    return results$;
  }());
};

Can we kill that useless closure?

summivox avatar Feb 20 '16 22:02 summivox

This test

# Closure-wrapped comprehensions that refer to the "arguments" object
expr = ->
  result = [item * item for item in arguments]

ok expr(2, 4, 8).join(' ') is '4 16 64'

Again it doesn't matter whether it is compiled to a closure or not.

BTW: leaking arguments is a bad idea anyway (#857).

summivox avatar Feb 21 '16 06:02 summivox