Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created February 20, 2014 19:41
Show Gist options
  • Save pvdz/9121615 to your computer and use it in GitHub Desktop.
Save pvdz/9121615 to your computer and use it in GitHub Desktop.
for-in rewrite rules.
// var version, can only have one var with a limited initializer
// can safely reuse the new var because it can't have side effects
for (var key=init in o) stmt;
// =>
var key = init, _γ_keys = [], _γ_expro = expr;
for (key in _γ_expro) _γ_keys.push(key);
while (_γ_keys.length) if (expro.hasOwnProperty(key = _γ_keys.shift())) stmt;
// non-var version, lhs can be any single expression
// we have to create our own temp var because lhs is not "safe"
// makes hasOwnProp check more tedious because we have to postpone key assignment
for (lhs in expr) stmt;
// =>
lhs;
var _γ_key, _γ_keys = [], _γ_expr_o = expr, _γ_tkey;
for (_γ_key in _γ_expro) _γ_keys.push(_γ_key);
while (_γ_keys.length) if (_γ_expro.hasOwnProperty(_γ_tkey = _γ_keys.shift())) { lhs = _γ_tkey; stmt; }
@pvdz
Copy link
Author

pvdz commented Feb 20, 2014

Have this working now. Result is a bit different:

      var o = {foo:1, bar:3, 15:"cant"};
      function* f(){
        var key = init ;
var _γ_key, _γ_keys = [], _γ_expro = ( o), _γ_tkey;
for (_γ_key in _γ_expro) _γ_keys.push(_γ_key);
while (_γ_keys.length) 
  if (_γ_expro.hasOwnProperty(_γ_tkey = _γ_keys.shift()) && !void( key = blabdsad  = _γ_tkey)) 
 {
          yield key;
        }
      }

Basically hacked the ultimate key assignment into the if condition so that I wouldn't have to bother with wrapping the for-statement in block brackets. Could still do that of course, just more work for no other reason than human legibility. Which is already zilch :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment