16 | 16 |
(= i 1) children
|
17 | 17 |
:else not-found)))
|
18 | 18 |
|
19 | |
(defn make-rose [root children]
|
|
19 |
(defn root
|
|
20 |
"Returns the root of a Rose tree."
|
|
21 |
{:no-doc true}
|
|
22 |
[rose]
|
|
23 |
(.-root rose))
|
|
24 |
|
|
25 |
(defn children
|
|
26 |
"Returns the children of the root of the Rose tree."
|
|
27 |
{:no-doc true}
|
|
28 |
[rose]
|
|
29 |
(.-children rose))
|
|
30 |
|
|
31 |
(defn make-rose
|
|
32 |
[root children]
|
20 | 33 |
(RoseTree. root children))
|
21 | 34 |
|
22 | 35 |
(defn- exclude-nth
|
|
33 | 46 |
"Turn a tree of trees into a single tree. Does this by concatenating
|
34 | 47 |
children of the inner and outer trees."
|
35 | 48 |
{:no-doc true}
|
36 | |
[[[inner-root inner-children] children]]
|
37 | |
(make-rose
|
38 | |
inner-root
|
39 | |
(concat (map join children)
|
40 | |
inner-children)))
|
|
49 |
[rose]
|
|
50 |
(let [outer-root (root rose)
|
|
51 |
outer-children (children rose)
|
|
52 |
inner-root (root outer-root)
|
|
53 |
inner-children (children outer-root)]
|
|
54 |
(make-rose inner-root (concat (map join outer-children)
|
|
55 |
inner-children))))
|
41 | 56 |
|
42 | |
(defn root
|
43 | |
"Returns the root of a Rose tree."
|
44 | |
{:no-doc true}
|
45 | |
[tree]
|
46 | |
(nth tree 0))
|
47 | |
|
48 | |
(defn children
|
49 | |
"Returns the children of the root of the Rose tree."
|
50 | |
{:no-doc true}
|
51 | |
[tree]
|
52 | |
(nth tree 1))
|
53 | 57 |
|
54 | 58 |
(defn pure
|
55 | 59 |
"Puts a value `x` into a Rose tree, with no children."
|
|
60 | 64 |
(defn fmap
|
61 | 65 |
"Applies functions `f` to all values in the tree."
|
62 | 66 |
{:no-doc true}
|
63 | |
[f [root children]]
|
64 | |
(make-rose (f root) (map (partial fmap f) children)))
|
|
67 |
[f rose]
|
|
68 |
(make-rose (f (root rose)) (map #(fmap f %) (children rose))))
|
65 | 69 |
|
66 | 70 |
(defn bind
|
67 | 71 |
"Takes a Rose tree (m) and a function (k) from
|
|
76 | 80 |
do not pass `pred` have their children cut out as well.
|
77 | 81 |
Takes a list of roses, not a rose"
|
78 | 82 |
{:no-doc true}
|
79 | |
[pred [the-root children]]
|
80 | |
(make-rose
|
81 | |
the-root
|
82 | |
(map (partial filter pred)
|
83 | |
(core/filter (comp pred root) children))))
|
|
83 |
[pred rose]
|
|
84 |
(make-rose (root rose)
|
|
85 |
(map #(filter pred %)
|
|
86 |
(core/filter #(pred (root %)) (children rose)))))
|
84 | 87 |
|
85 | 88 |
(defn permutations
|
86 | 89 |
"Create a seq of vectors, where each rose in turn, has been replaced
|
87 | 90 |
by its children."
|
88 | 91 |
{:no-doc true}
|
89 | 92 |
[roses]
|
90 | |
(apply concat
|
91 | |
(for [[rose index]
|
92 | |
(map vector roses (range))]
|
93 | |
(for [child (children rose)] (assoc roses index child)))))
|
|
93 |
(for [[rose index] (map vector roses (range))
|
|
94 |
child (children rose)]
|
|
95 |
(assoc roses index child)))
|
94 | 96 |
|
95 | 97 |
(defn zip
|
96 | 98 |
"Apply `f` to the sequence of Rose trees `roses`."
|
97 | 99 |
{:no-doc true}
|
98 | 100 |
[f roses]
|
99 | 101 |
(make-rose
|
100 | |
(apply f (map root roses))
|
101 | |
(map (partial zip f)
|
102 | |
(permutations roses))))
|
|
102 |
(apply f (map root roses))
|
|
103 |
(map #(zip f %)
|
|
104 |
(permutations roses))))
|
103 | 105 |
|
104 | 106 |
(defn remove
|
105 | 107 |
{:no-doc true}
|
|
113 | 115 |
[f roses]
|
114 | 116 |
(if (core/seq roses)
|
115 | 117 |
(make-rose (apply f (map root roses))
|
116 | |
(map (partial shrink f) (remove roses)))
|
|
118 |
(map #(shrink f %) (remove roses)))
|
117 | 119 |
(make-rose (f) [])))
|
118 | 120 |
|
119 | 121 |
(defn collapse
|
|
121 | 123 |
are the children from depth one _and_ two of the input
|
122 | 124 |
tree."
|
123 | 125 |
{:no-doc true}
|
124 | |
[[root the-children]]
|
125 | |
(make-rose
|
126 | |
root
|
127 | |
(concat (map collapse the-children)
|
128 | |
(map collapse
|
129 | |
(mapcat children the-children)))))
|
|
126 |
[rose]
|
|
127 |
(make-rose (root rose)
|
|
128 |
(let [the-children (children rose)]
|
|
129 |
(concat (map collapse the-children)
|
|
130 |
(map collapse
|
|
131 |
(mapcat children the-children))))))
|
130 | 132 |
|
131 | 133 |
(defn- make-stack
|
132 | 134 |
[children stack]
|
|
141 | 143 |
fit that description. This function is significantly faster than
|
142 | 144 |
brute-force enumerating all of the nodes in a tree, as there will
|
143 | 145 |
be many duplicates."
|
144 | |
[root]
|
145 | |
(let [helper (fn helper [[node children] seen stack]
|
146 | |
(lazy-seq
|
147 | |
(if-not (seen node)
|
148 | |
(cons node
|
149 | |
(if (core/seq children)
|
150 | |
(helper (first children) (conj seen node) (make-stack (rest children) stack))
|
151 | |
(when-let [s (core/seq stack)]
|
152 | |
(let [f (ffirst s)
|
153 | |
r (rest (first s))]
|
154 | |
(helper f (conj seen node) (make-stack r (rest s)))))))
|
155 | |
(when-let [s (core/seq stack)]
|
156 | |
(let [f (ffirst s)
|
157 | |
r (rest (first s))]
|
158 | |
(helper f seen (make-stack r (rest s))))))))]
|
159 | |
(helper root #{} '())))
|
|
146 |
[rose]
|
|
147 |
(let [helper (fn helper [rose seen stack]
|
|
148 |
(let [node (root rose)
|
|
149 |
the-children (children rose)]
|
|
150 |
(lazy-seq
|
|
151 |
(if-not (seen node)
|
|
152 |
(cons node
|
|
153 |
(if (core/seq the-children)
|
|
154 |
(helper (first the-children) (conj seen node) (make-stack (rest the-children) stack))
|
|
155 |
(when-let [s (core/seq stack)]
|
|
156 |
(let [f (ffirst s)
|
|
157 |
r (rest (first s))]
|
|
158 |
(helper f (conj seen node) (make-stack r (rest s)))))))
|
|
159 |
(when-let [s (core/seq stack)]
|
|
160 |
(let [f (ffirst s)
|
|
161 |
r (rest (first s))]
|
|
162 |
(helper f seen (make-stack r (rest s)))))))))]
|
|
163 |
(helper rose #{} '())))
|