-
Notifications
You must be signed in to change notification settings - Fork 11
/
shri-async-hw.js
52 lines (41 loc) · 1.49 KB
/
shri-async-hw.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
49
50
51
(() => {
const _wrap = (fn, cb) => {
setTimeout(() => {
cb(fn());
}, Math.random() * 20);
};
const AsyncArray = function(initial) {
if (initial && !(initial instanceof Array)) {
throw new Error('initial value is not an array');
}
const a = initial ? Array.from(initial) : [];
this.set = (index, value, cb) => _wrap(() => { a[index] = value }, cb);
this.push = (value, cb) => _wrap(() => { a.push(value) }, cb);
this.get = (index, cb) => _wrap(() => a[index], cb);
this.pop = (cb) => _wrap(() => a.pop(), cb);
this.length = (cb) => _wrap(() => a.length, cb);
this.print = () => { console.log(a.toString()); };
}
const add = (a, b, cb) => _wrap(() => a + b, cb);
const subtract = (a, b, cb) => _wrap(() => a - b, cb);
const multiply = (a, b, cb) => _wrap(() => a * b, cb);
const divide = (a, b, cb) => _wrap(() => a / b, cb);
const mod = (a, b, cb) => _wrap(() => a % b, cb);
const less = (a, b, cb) => _wrap(() => a < b, cb);
const equal = (a, b, cb) => _wrap(() => a == b, cb);
const lessOrEqual = (a, b, cb) => _wrap(() => a <= b, cb);
const sqrt = (x, cb) => _wrap(() => Math.sqrt(x), cb);
window.Homework = {
AsyncArray,
add,
subtract,
multiply,
divide,
mod,
less,
equal,
lessOrEqual,
sqrt
};
Object.freeze(window.Homework);
})();