Last active
August 29, 2015 14:05
-
-
Save aeharding/7402e718de8359060761 to your computer and use it in GitHub Desktop.
Sort an array of positive integers using timeouts. *genius*
This file contains 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
# Only works with positive numbers #dealwithit | |
timeoutSort = (arr, cb) -> | |
ret = [] | |
longest = 0 | |
for t in arr then do -> | |
longest = t if t > longest | |
tmp = t | |
setTimeout -> | |
ret.push tmp | |
, t | |
setTimeout (-> cb? ret), longest | |
return | |
# Tests FTW | |
timeoutSort [2, 45, 23, 94, 201], (ret) -> | |
console.assert Array.isArray ret, 'ret is array' | |
console.assert ret.length is 5, 'five elements long' | |
expected = [2, 23, 45, 94, 201] | |
i = expected.length | |
while i-- | |
console.assert expected[i] is ret[i], "item at index #{i} expected #{expected[i]}, got #{ret[i]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment