-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patharray.gates
More file actions
39 lines (34 loc) · 1.25 KB
/
array.gates
File metadata and controls
39 lines (34 loc) · 1.25 KB
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
(function () {
assert_eq([ 1, 4, 9 ], map(function (n, i) {
assert_eq(n - 1, i);
return n * n;
}, [ 1, 2, 3 ]));
assert_eq([ 2, 4 ], filter(function (n, i) {
assert_eq(n - 1, i);
return n % 2 == 0;
}, [ 1, 2, 3, 4 ]));
{
let base = [ 1, 2, 3, 4 ];
let first = true;
assert_eq(10, reduce(function (acc, current, i, array) {
if (first) {
assert_eq(0, acc);
assert_eq(1, current);
first = false;
}
assert_eq(current - 1, i);
assert_eq(base, array);
return acc + current;
}, 0, base));
}
assert_eq(null, reduce(function () { return 1; }, null, []));
assert_eq(2, find(function (v) { return v % 2 == 0; }, [ 1, 2, 3, 4 ]));
assert_eq(null, find(function (v) { return false; }, []));
assert_eq(4, find_last(function (v) { return v % 2 == 0; }, [ 1, 2, 3, 4 ]));
assert_eq(null, find_last(function (v) { return false; }, []));
assert_eq(1, find_index(function (v) { return v % 2 == 0; }, [ 1, 2, 3, 4 ]));
assert_eq(-1, find_index(function (v) { return false; }, []));
assert_eq(3, find_last_index(function (v) { return v % 2 == 0; }, [ 1, 2, 3, 4 ]));
assert_eq(-1, find_last_index(function (v) { return false; }, []));
assert_eq([], null | map(() => {}));
})()