using push for comprehension results so that it works with object keys, and adding a test for object comprehensions

This commit is contained in:
Jeremy Ashkenas
2009-12-31 15:03:32 -05:00
parent 3042a50f87
commit f7e49eaae4
4 changed files with 11 additions and 9 deletions

View File

@@ -574,8 +574,8 @@ module CoffeeScript
body = @body
suffix = ';'
set_result = "#{rvar} = [];\n#{o[:indent]}"
save_result = "#{rvar}[#{index_var || ivar}] = "
return_result = rvar
body = CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body])
if o[:return] || o[:assign]
return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign]
@@ -583,9 +583,7 @@ module CoffeeScript
o.delete(:assign)
o.delete(:return)
if @filter
body = CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body])
body = IfNode.new(@filter, body, nil, :statement => true)
save_result = ''
suffix = ''
end
elsif @filter
@@ -594,7 +592,7 @@ module CoffeeScript
return_result = "\n#{o[:indent]}#{return_result};"
body = body.compile(o.merge(:indent => body_dent))
write("#{source_part}#{set_result}for (#{for_part}) {#{pre_cond}#{var_part}\n#{body_dent}#{save_result}#{body}#{suffix}#{post_cond}\n#{o[:indent]}}#{return_result}")
write("#{source_part}#{set_result}for (#{for_part}) {#{pre_cond}#{var_part}\n#{body_dent}#{body}#{suffix}#{post_cond}\n#{o[:indent]}}#{return_result}")
end
end

View File

@@ -1,8 +1,12 @@
nums: n * n for n in [1, 2, 3] when n % 2 isnt 0
results: n * 2 for n in nums
obj: {one: 1, two: 2, three: 3}
names: key + '!' for value, key in obj
# next: for n in [1, 2, 3] if n % 2 isnt 0
# print('hi') if false
# n * n * 2
print(results.join(',') is '2,18')
print(results.join(',') is '2,18')
print(names.join(' ') is "one! two! three!")

View File

@@ -14,7 +14,7 @@
for (i in __a) {
if (__a.hasOwnProperty(i)) {
item = __a[i];
__b[i] = iterator.call(context, item, i, obj);
__b.push(iterator.call(context, item, i, obj));
}
}
__b;
@@ -24,7 +24,7 @@
for (__d in __c) {
if (__c.hasOwnProperty(__d)) {
key = __c[__d];
__e[__d] = iterator.call(context, obj[key], key, obj);
__e.push(iterator.call(context, obj[key], key, obj));
}
}
__e;

View File

@@ -13,7 +13,7 @@ _.each = function each(obj, iterator, context) {
for (i in __a) {
if (__a.hasOwnProperty(i)) {
item = __a[i];
__b[i] = iterator.call(context, item, i, obj);
__b.push(iterator.call(context, item, i, obj));
}
}
__b;
@@ -23,7 +23,7 @@ _.each = function each(obj, iterator, context) {
for (__d in __c) {
if (__c.hasOwnProperty(__d)) {
key = __c[__d];
__e[__d] = iterator.call(context, obj[key], key, obj);
__e.push(iterator.call(context, obj[key], key, obj));
}
}
__e;