-
Notifications
You must be signed in to change notification settings - Fork 1
/
calgen.js
48 lines (45 loc) · 1.05 KB
/
calgen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//6+4+2+1-3=16
//6+4+2+1-3=10
//6+4+2-1=11
//6+4-2=8
//6-4=2
var res = new Int16Array([16,10,11,8,2]),l= res.length;
var variables = [];
var Q = require('q');
var qTail = Q.denodeify(calculateTail);
var qHead = Q.denodeify(calculateHead);
(function calculate(i) {
Q.spawn(function* () {
i = yield Q
.all([qTail(res[i],res[i+1]),qHead(res[i],res[i+1])])
.spread(function(tail,head){
variables[i] = tail;
res[i+1] = head;
return i+1;
})
console.log('-----------------'+i+'-----------------')
if(i === l-1) {
variables[i] = res[i];
console.log(i + ":" + variables[i]);
process.exit();
}else {
calculate(i);
}
});
})(0);
function calculateTail(x,y,cb) {
console.log("calculate tail:("+ x + "," + y + ")");
setTimeout(function(){
var tail = (x-y)/2;
console.log("tail:" + tail);
cb(null,tail);
},300);
}
function calculateHead(x,y,cb) {
console.log("calculate head:("+ x + "," + y + ")");
setTimeout(function(){
var head = (x+y)/2;
console.log("head:" + head);
cb(null,head);
},400);
}