blob: 65b46e382615fd7d63586de9dec969edb04571b4 (
plain)
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
|
(.module:
[lux #*
data/text/format
["_" test (#+ Test)]
[abstract
[monad (#+ do)]
{[0 #test]
[/
["$." equivalence]
["$." fold]
["$." functor]]}]
[data
[number
["." nat]]
[collection
["." list ("#@." functor fold)]]]
[math
["r" random (#+ Random)]]]
{1
["." / (#+ Tree)]})
(def: #export (tree size gen-value)
(All [a] (-> Nat (Random a) (Random (Tree a))))
(let [singleton (:: r.monad map /.leaf gen-value)]
(case size
0
singleton
1
singleton
2
(do r.monad
[value gen-value
single (tree 1 gen-value)]
(wrap (/.branch value (list single))))
_
(do r.monad
[value gen-value
#let [size (dec size)]
left (tree (n// 2 size) gen-value)
right (tree (n/+ (n/% 2 size) (n// 2 size))
gen-value)]
(wrap (/.branch value (list left right))))
)))
(def: #export test
Test
(<| (_.context (%name (name-of /.Tree)))
(do r.monad
[size (:: @ map (|>> (n/% 100) (n/+ 1)) r.nat)]
($_ _.and
($equivalence.spec (/.equivalence nat.equivalence) (..tree size r.nat))
($fold.spec /.leaf /.equivalence /.fold)
($functor.spec /.leaf /.equivalence /.functor)
(do @
[sample (..tree size r.nat)]
(_.test "Can flatten a tree to get all the nodes as a flat tree."
(n/= size
(list.size (/.flatten sample)))))
))))
|