-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
43 lines (35 loc) · 773 Bytes
/
test.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
'lang sweet.js';
import { class, protocol, implements } from './src/index';
protocol Foldable {
foldr;
toArray() {
return this[Foldable.foldr](
(m, a) => [a].concat(m),
[]
);
}
get length() {
return this[Foldable.foldr](m => m + 1, 0);
}
contains(eq, e) {
return this[Foldable.foldr](
(m, a) => m || eq(a, e),
false
);
}
}
class NEList implements Foldable {
constructor(head, tail) {
this.head = head;
this.tail = tail;
}
[Foldable.foldr](f, memo) {
if (this.tail != null) memo =
this.tail[Foldable.foldr](f, memo);
return f(memo, this.head);
}
}
let a = new NEList(1, null);
let b = new NEList(0, a);
console.log(b[Foldable.toArray]());
console.log(b[Foldable.length]);