@@ -6,7 +6,7 @@ import Control.Comonad.Cofree (head, (:<))
66import Data.List (List(Nil), (:))
77import Data.Maybe (Maybe(..), fromJust)
88import Data.Tree (Tree, mkTree, scanTree)
9−import Data.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, insertAfter, modifyValue, next, toTree, value)
9+import Data.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, insertAfter, modifyValue, next, toTree, value, firstChild, lastChild)
1010import Effect (Effect)
1111import Partial.Unsafe (unsafePartial)
1212import Test.Spec (describe, it)
@@ -15,13 +15,13 @@ import Test.Spec.Reporter (consoleReporter)
1515import Test.Spec.Runner (run)
1616
1717sampleTree :: Tree Int
18−sampleTree =
19− 1 :<
18+sampleTree =
19+ 1 :<
2020 (2 :< Nil)
2121 : (3 :< Nil)
2222 : (4 :<
2323 (5 :< Nil)
24− : (6 :<
24+ : (6 :<
2525 (7 :< Nil) : Nil)
2626 : (8 :< Nil)
2727 : Nil
@@ -29,21 +29,21 @@ sampleTree =
2929 : Nil
3030
3131main :: Effect Unit
32−main = run [consoleReporter] do
33− describe "Tree" do
32+main = run [consoleReporter] do
33+ describe "Tree" do
3434
3535 it "mkTree" do
3636 let t = mkTree 10 Nil
3737 shouldEqual (head t) 10
3838
3939 it "Functor" do
40− let result =
41− 2 :<
40+ let result =
41+ 2 :<
4242 (3 :< Nil)
4343 : (4 :< Nil)
4444 : (5 :<
4545 (6 :< Nil)
46− : (7 :<
46+ : (7 :<
4747 (8 :< Nil) : Nil)
4848 : (9 :< Nil)
4949 : Nil
@@ -52,35 +52,35 @@ main = run [consoleReporter] do
5252 shouldEqual (eq (((+)1) <$> sampleTree) result) true
5353
5454 it "scanTree" do
55− let result =
56− 1 :<
55+ let result =
56+ 1 :<
5757 (3 :< Nil)
5858 : (4 :< Nil)
5959 : (5 :<
6060 (10 :< Nil)
61− : (11 :<
61+ : (11 :<
6262 (18 :< Nil) : Nil)
6363 : (13 :< Nil)
6464 : Nil
6565 )
6666 : Nil
6767 shouldEqual (eq (scanTree (\a b -> a + b) 0 sampleTree) result) true
68−
68+
6969 describe "Zipper" do
7070
7171 let root = fromTree sampleTree
7272
7373 it "Modify" do
7474
75− let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
76− let root'' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down >=> next >=> next >=> down $ root)
75+ let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
76+ let root'' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down >=> next >=> next >=> down $ root)
7777 let result =
78− 1 :<
78+ 1 :<
7979 (4 :< Nil)
8080 : (3 :< Nil)
8181 : (4 :<
8282 (5 :< Nil)
83− : (6 :<
83+ : (6 :<
8484 (7 :< Nil) : Nil)
8585 : (8 :< Nil)
8686 : Nil
@@ -88,12 +88,12 @@ main = run [consoleReporter] do
8888 : Nil
8989
9090 let result' =
91− 1 :<
91+ 1 :<
9292 (2 :< Nil)
9393 : (3 :< Nil)
9494 : (4 :<
9595 (10 :< Nil)
96− : (6 :<
96+ : (6 :<
9797 (7 :< Nil) : Nil)
9898 : (8 :< Nil)
9999 : Nil
@@ -102,42 +102,74 @@ main = run [consoleReporter] do
102102
103103 shouldEqual (eq root' result) true
104104 shouldEqual (eq root'' result') true
105−
105+
106106 it "Insert" do
107107
108− let root' = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
109− let root'' = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
108+ let root1 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
109+ let root2 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
110+ let root3 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (firstChild root))
111+ let root4 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (lastChild root))
110112
111− let result =
112− 1 :<
113+ let result1 =
114+ 1 :<
113115 (2 :< Nil)
114116 : (100 :< Nil)
115117 : (3 :< Nil)
116118 : (4 :<
117119 (5 :< Nil)
118− : (6 :<
120+ : (6 :<
119121 (7 :< Nil) : Nil)
120122 : (8 :< Nil)
121123 : Nil
122124 )
123125 : Nil
124126
125− let result' =
126− 1 :<
127+ let result2 =
128+ 1 :<
127129 (2 :< Nil)
128130 : (3 :< Nil)
129131 : (4 :<
130132 (5 :< Nil)
131− : (6 :<
132− (7 :< Nil)
133+ : (6 :<
134+ (7 :< Nil)
133135 : (100 :< Nil)
134136 : Nil)
135137 : (8 :< Nil)
136138 : Nil
137139 )
138140 : Nil
139− shouldEqual (eq root' result) true
140− shouldEqual (eq root'' result') true
141+ let result3 =
142+ 1 :<
143+ (2 :< Nil)
144+ : (100 :< Nil)
145+ : (3 :< Nil)
146+ : (4 :<
147+ (5 :< Nil)
148+ : (6 :<
149+ (7 :< Nil)
150+ : Nil
151+ )
152+ : (8 :< Nil)
153+ : Nil
154+ )
155+ : Nil
156+ let result4 =
157+ 1 :<
158+ (2 :< Nil)
159+ : (3 :< Nil)
160+ : (4 :<
161+ (5 :< Nil)
162+ : (6 :<
163+ (7 :< Nil) : Nil)
164+ : (8 :< Nil)
165+ : Nil
166+ )
167+ : (100 :< Nil)
168+ : Nil
169+ shouldEqual (eq root1 result1) true
170+ shouldEqual (eq root2 result2) true
171+ shouldEqual (eq root3 result3) true
172+ shouldEqual (eq root4 result4) true
141173
142174 it "Should findDownWhere with single node" do
143175 let tree = 1 :< Nil
@@ -145,96 +177,96 @@ main = run [consoleReporter] do
145177 shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
146178
147179 it "Should findDownWhere with 2 nodes and 2 levels" do
148− let tree = 1 :<
149− (2 :< Nil)
180+ let tree = 1 :<
181+ (2 :< Nil)
150182 : Nil
151183 -- log $ showTree tree
152184 let loc = fromTree tree
153185 shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
154186
155187 it "Should findDownWhere with 3 nodes and 2 levels" do
156− let tree = 1 :<
157− (2 :< Nil)
158− : (3 :< Nil)
188+ let tree = 1 :<
189+ (2 :< Nil)
190+ : (3 :< Nil)
159191 : Nil
160192 -- log $ showTree tree
161193 let loc = fromTree tree
162194 shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
163195
164196 it "Should findDownWhere with 4 nodes and 2 levels" do
165− let tree = 1 :<
166− (2 :< Nil)
167− : (3 :< Nil)
168− : (4 :< Nil)
197+ let tree = 1 :<
198+ (2 :< Nil)
199+ : (3 :< Nil)
200+ : (4 :< Nil)
169201 : Nil
170202 -- log $ showTree tree
171203 let loc = fromTree tree
172204 shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
173205
174206 it "Should findDownWhere with 5 nodes and 3 levels" do
175− let tree = 1 :<
176− (2 :< Nil)
177− : (3 :< Nil)
178− : (4 :<
207+ let tree = 1 :<
208+ (2 :< Nil)
209+ : (3 :< Nil)
210+ : (4 :<
179211 (5 :< Nil)
180− : Nil)
212+ : Nil)
181213 : Nil
182214 -- log $ showTree tree
183215 let loc = fromTree tree
184216 shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
185217
186218 it "Should findDownWhere with 6 nodes and 3 levels" do
187− let tree = 1 :<
188− (2 :< Nil)
189− : (3 :< Nil)
190− : (4 :<
219+ let tree = 1 :<
220+ (2 :< Nil)
221+ : (3 :< Nil)
222+ : (4 :<
191223 (5 :< Nil)
192224 : (6 :< Nil)
193− : Nil)
225+ : Nil)
194226 : Nil
195227 -- log $ showTree tree
196228 let loc = fromTree tree
197229 shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
198230
199231 it "Should findDownWhere with 7 nodes and 4 levels" do
200− let tree = 1 :<
201− (2 :< Nil)
202− : (3 :< Nil)
203− : (4 :<
232+ let tree = 1 :<
233+ (2 :< Nil)
234+ : (3 :< Nil)
235+ : (4 :<
204236 (5 :< Nil)
205− : (6 :<
237+ : (6 :<
206238 (7 :< Nil) : Nil)
207− : Nil)
239+ : Nil)
208240 : Nil
209241 -- log $ showTree tree
210242 let loc = fromTree tree
211243 shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
212244
213245 it "Should findDownWhere with 8 nodes and 4 levels" do
214− let tree = 1 :<
215− (2 :< Nil)
216− : (3 :< Nil)
217− : (4 :<
246+ let tree = 1 :<
247+ (2 :< Nil)
248+ : (3 :< Nil)
249+ : (4 :<
218250 (5 :< Nil)
219− : (6 :<
251+ : (6 :<
220252 (7 :< Nil) : Nil)
221253 : (8 :< Nil)
222− : Nil)
254+ : Nil)
223255 : Nil
224256 -- log $ showTree tree
225257 let loc = fromTree tree
226258 shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
227259
228260 it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
229− let tree = 1 :<
230− (2 :< Nil)
231− : (3 :< Nil)
232− : (4 :<
261+ let tree = 1 :<
262+ (2 :< Nil)
263+ : (3 :< Nil)
264+ : (4 :<
233265 (5 :< Nil)
234− : (6 :<
266+ : (6 :<
235267 (7 :< Nil) : Nil)
236268 : (8 :< Nil)
237− : Nil)
269+ : Nil)
238270 : Nil
239271 -- log $ showTree tree
240272 let loc = fromTree tree
@@ -260,6 +292,3 @@ main = run [consoleReporter] do
260292 --log $ showTree sampleTree
261293 --log $ show flat
262294 shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)
263−
264−
265−