Object comprehension inside array comprehension - inconsistent compilation?

38 views
Skip to first unread message

Piotr Klibert

unread,
Aug 6, 2014, 3:53:19 PM8/6/14
to lives...@googlegroups.com
Hello,

Recently I needed to rename some attributes on a couple of objects, which I got a list of. I did it like this (where f takes original name and returns new name):

new-obj-list = [ {[f(k), v] for k,v of obj} for obj in obj-list]

this didn't work - I got an array of arrays, where all the inner arrays had additional properties set to them. Compiled JS looks like this:

var newObjList, res$, i$, ref$, len$, obj, lresult$, k, v;
res$
= [];
for (i$ = 0, len$ = (ref$ = objList).length; i$ < len$; ++i$) {
  obj
= ref$[i$];
  lresult$
= [];
 
for (k in obj) {
    v
= obj[k];
    lresult$
[f(k)] = v;
 
}
  res$
.push(lresult$);
}
newObjList
= res$;

It looks like `lresult$` is initialized with an empty Array instead of empty Object.

Normal nested loops are compiled to the same code:

new-obj-list = for obj in obj-list
    { [f(k), v] for k,v of obj }

var newObjList, res$, i$, ref$, len$, obj, lresult$, k, v;
res$ = [];
for (i$ = 0, len$ = (ref$ = objList).length; i$ < len$; ++i$) {
  obj = ref$[i$];
  lresult$ = [];
  for (k in obj) {
    v = obj[k];
    lresult$[f(k)] = v;
  }
  res$.push(lresult$);
}
newObjList = res$;

But when I do something - anything - with the inner obj comprehension it starts to work:

new-obj-list = for obj in obj-list
    z = { [f(k), v] for k,v of obj }


var newObjList, res$, i$, ref$, len$, obj, z, k, v;
res$ = [];
for (i$ = 0, len$ = (ref$ = objList).length; i$ < len$; ++i$) {
  obj = ref$[i$];
  res$.push(z = (fn$()));
}
newObjList = res$;
function fn$(){
  var ref$, results$ = {};
  for (k in ref$ = obj) {
    v = ref$[k];
    results$[f(k)] = v;
  }
  return results$;
}

Calling identity function on the inner obj comprehension works too.

Is this an expected behaviour?

Isiah Meadows

unread,
Jan 30, 2015, 5:01:29 AM1/30/15
to lives...@googlegroups.com
File a bug at https://github.com/gkz/LiveScript/issues.

Note that I've already found other issues with object literals, particularly where a condition gets duplicated in the output, which is bad.

Piotr Klibert

unread,
Jan 30, 2015, 8:09:43 AM1/30/15
to lives...@googlegroups.com
You do realize that this post is half a year old?

I since filed a bug, fixed it, created a PR and got it merged into LiveScript...
Reply all
Reply to author
Forward
0 new messages