Created
October 5, 2015 11:22
-
-
Save zapu/6c05772716331713b2c2 to your computer and use it in GitHub Desktop.
iced-coffee-script loop problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delay = (func) -> | |
setTimeout func, 1 | |
arrEq = (a, b) -> | |
r = a.length is b.length and a.every (elem, i) -> elem is b[i] | |
if not r | |
console.log 'arrays not equal', a, '!=', b | |
console.trace() | |
await delay defer() | |
# Step function should be called exactly once. | |
makeFunc = -> | |
calld = false | |
return -> | |
if calld | |
console.log 'step function called twice' | |
console.trace() | |
calld = true | |
return 2 | |
expected = [1,3] | |
func = makeFunc() | |
arr = [] | |
for x in [1,2,3] by func() | |
arr.push x | |
arrEq expected, arr | |
was_called = false | |
func = -> | |
was_called = true | |
return 2 | |
# function will not be called because specified step | |
# is ignored and 1 is used | |
arr = [] | |
for x in [1,2,3] by func() | |
await delay defer() | |
arr.push x | |
if not was_called | |
console.log 'function was not called' | |
console.trace() | |
arrEq expected, arr | |
func = makeFunc() | |
arr = [] | |
for x in [1..3] by func() | |
arr.push x | |
arrEq expected, arr | |
func = makeFunc() | |
arr = [] | |
for x in [1..3] by func() | |
await delay defer() | |
arr.push x | |
arrEq expected, arr | |
# Strides with steps that do not match direction of range are weird in | |
# coffee. Still, iced should mimic whatever coffee does. | |
for x in [1..3] by -1 | |
console.log 'should never get here' | |
console.trace() | |
break | |
for x in [1..3] by -1 | |
await delay defer() | |
console.log 'should never get here' | |
console.trace() | |
break | |
for x in [3..1] by 1 | |
console.log 'should never get here' | |
console.trace() | |
break | |
for x in [3..1] by 1 | |
await delay defer() | |
console.log 'should never get here' | |
console.trace() | |
break | |
# Without specified step, it is set to match range. | |
expected = [3,2,1] | |
arr = [] | |
for x in [3..1] | |
arr.push x | |
arrEq expected, arr | |
arr = [] | |
for x in [3..1] | |
await delay defer() | |
arr.push x | |
arrEq expected, arr | |
# Should also work with exclusive ranges | |
expected = [3,2] | |
arr = [] | |
for x in [3...1] | |
arr.push x | |
arrEq expected, arr | |
arr = [] | |
for x in [3...1] | |
await delay defer() | |
arr.push x | |
arrEq expected, arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment