forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.ts
76 lines (68 loc) · 1.59 KB
/
16.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// Code for http://www.tomharding.me/2017/06/12/fantas-eel-and-specification-16/
//
export const URI = 'RoseTree'
export type URI = typeof URI
export class RoseTree<A> {
readonly '-A': A
readonly '-URI': URI
constructor(readonly root: A, readonly forest: Array<RoseTree<A>>) {}
map<B>(f: (a: A) => B): RoseTree<B> {
return new RoseTree(f(this.root), this.forest.map(rt => rt.map(f)))
}
extend<B>(f: (rt: RoseTree<A>) => B): RoseTree<B> {
return new RoseTree(f(this), this.forest.map(rt => rt.extend(f)))
}
}
const myTree = new RoseTree(1, [new RoseTree(2, []), new RoseTree(3, [new RoseTree(4, [])])])
console.log(
JSON.stringify(
myTree.extend(rt => {
return rt.forest.length < 1
? { value: rt.root, color: 'RED' }
: rt.forest.length < 5 ? { value: rt.root, color: 'ORANGE' } : { value: rt.root, color: 'GREEN' }
}),
null,
2
)
)
/*
{
"root": {
"value": 1,
"color": "ORANGE"
},
"forest": [
{
"root": {
"value": 2,
"color": "RED"
},
"forest": []
},
{
"root": {
"value": 3,
"color": "ORANGE"
},
"forest": [
{
"root": {
"value": 4,
"color": "RED"
},
"forest": []
}
]
}
]
}
*/
import { array, fold } from '../src/Array'
const temperatures = [23, 19, 19, 18, 18, 20, 24]
console.log(
array.extend(temperatures, as =>
fold(as, '???', (prev, tail) => fold(tail, '???', (next, _) => (next <= prev ? 'YAY' : 'BOO')))
)
)
// => [ 'YAY', 'YAY', 'YAY', 'YAY', 'BOO', 'BOO', '???' ]