Skip to content

Instantly share code, notes, and snippets.

@tiye
Created August 14, 2012 14:47
Show Gist options
  • Select an option

  • Save tiye/3349934 to your computer and use it in GitHub Desktop.

Select an option

Save tiye/3349934 to your computer and use it in GitHub Desktop.
comparation
compareAsync = (x, y) ->
sleep 10 # 暂停10毫秒
x - y
swapAsync = (a, i, j) ->
sleep 20 # 暂停20毫秒
t = a[i]
a[i] = a[j]
a[j] = t
paint a # 重绘数组
bubbleSortAsync = (array) ->
for i in array
for j in array[...i-1]
# 异步比较元素
r = compareAsync array[j], array[j + 1]
# 异步交换元素
if r > 0 then swapAsync array, j, j + 1
var compareAsync = eval(Wind.compile("async", function (x, y) {
$await(Wind.Async.sleep(10)); // 暂停10毫秒
return x - y;
}));
var swapAsync = eval(Wind.compile("async", function (a, i, j) {
$await(Wind.Async.sleep(20)); // 暂停20毫秒
var t = a[i]; a[i] = a[j]; a[j] = t;
paint(a); // 重绘数组
}));
var bubbleSortAsync = eval(Wind.compile("async", function (array) {
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array.length - i - 1; j++) {
// 异步比较元素
var r = $await(compareAsync(array[j], array[j + 1]));
// 异步交换元素
if (r > 0) $await(swapAsync(array, j, j + 1));
}
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment