Skip to content

Commit

Permalink
fixes #285: filtered comprehensions over ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Mar 22, 2014
1 parent 31b8751 commit f8df1d7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/compiler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/compiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ class exports.Compiler
((@target.right.instanceof CS.Int) or ((@target.right.instanceof CS.UnaryNegateOp) and @target.right.expression.instanceof CS.Int))
varDeclaration = new JS.AssignmentExpression '=', i, compile @target.left
update = new JS.UpdateExpression '++', yes, i
if @filter?
block.body.unshift stmt new JS.IfStatement (new JS.UnaryExpression '!', filter), new JS.ContinueStatement
if keyAssignee?
k = genSym 'k'
varDeclaration = new JS.SequenceExpression [(new JS.AssignmentExpression '=', k, new JS.Literal 0), varDeclaration]
Expand Down
13 changes: 13 additions & 0 deletions test/comprehensions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ suite 'Comprehensions', ->
arrayEq [a..b], (fn() for in [a..b])
c = 0
arrayEq [a...b], (fn() for in [a...b])

test 'filtered comprehensions', ->
list = [0..5]
filtered = (a for a in list when a & 1)
arrayEq filtered, [1, 3, 5]
filtered = (a for a in list when a < 4)
arrayEq filtered, [0..3]

test '#285: filtered comprehensions over ranges', ->
filtered = (a for a in [0..5] when a & 1)
arrayEq filtered, [1, 3, 5]
filtered = (a for a in [0..5] when a < 4)
arrayEq filtered, [0..3]

0 comments on commit f8df1d7

Please sign in to comment.