git.iko.soy/rowtype-yoga/purescript-yoga-tree
on master help sign in
got rid of the warnings
commit591a7d4f58f74c4e5bab4750f3032e2e664565a7 authorDustin Whitney <dustin.whitney@gmail.com> dateMon 21 May 2018, 10:15 (8 years ago) parent3dc610d
3 files changed+29−5
src/Data/Tree.purs+3−3 src/Data/Tree/Zipper.purs+16 test/Main.purs+10−2
src/Data/Tree.purs+3−3
@@ -21,7 +21,7 @@ mkTree = mkCofree
2121drawTree :: Tree String -> String
2222drawTree t = tailRec go {level: 0, drawn: (head t) <> "\n", current: (tail t)}
2323 where
24− go :: _ -> Step _ String
24+ go :: { current :: List (Tree String) , drawn :: String , level :: Int } -> Step { current :: List (Tree String) , drawn :: String , level :: Int } String
2525 go {level: l, drawn: s, current: Nil} = Done s
2626 go {level: l, drawn: s, current: c:cs } =
2727 let drawn = (power " " l) <> "|----> " <> (head c) <> "\n" in
@@ -39,7 +39,7 @@ scanTree f b n =
3939 let fb = f (head n) b
4040 in fb :< (tailRec go {b: fb, current: (tail n), final: Nil})
4141 where
42− go :: _ -> Step _ (Forest b)
42+ go :: { final :: List (Cofree List b) , current :: List (Cofree List a) , b :: b } -> Step { final :: List (Cofree List b) , current :: List (Cofree List a) , b :: b } (Forest b)
4343 go {b: b', current: Nil, final: final} = Done final
4444 go {b: b', current: c:cs, final: final} =
4545 let fb' = f (head c) b'
@@ -52,7 +52,7 @@ scanTreeAccum f b n =
5252 let fb = f (head n) b
5353 in fb.value :< (tailRec go {b: fb.accum , current: (tail n), final: Nil})
5454 where
55− go :: _ -> Step _ (Forest c)
55+ go :: { final :: List (Cofree List c) , current :: List (Cofree List a) , b :: b } -> Step { final :: List (Cofree List c) , current :: List (Cofree List a) , b :: b } (Forest c)
5656 go {b: b', current: Nil, final: final} = Done final
5757 go {b: b', current: c:cs, final: final} =
5858 let fb' = f (head c) b'
src/Data/Tree/Zipper.purs+16
@@ -264,6 +264,22 @@ findFromRootWhere predicate loc = findDownWhere predicate $ root loc
264264findFromRoot :: forall a. Eq a => a -> Loc a -> Maybe (Loc a)
265265findFromRoot a = findFromRootWhere (_ == a)
266266
267+flattenLocDepthFirst :: ∀ a. Loc a -> List (Loc a)
268+flattenLocDepthFirst loc = loc : (go loc)
269+ where
270+ go :: Loc a -> List (Loc a)
271+ go goLoc =
272+ let
273+ downs = goDir goLoc down
274+ nexts = goDir goLoc next
275+ in
276+ downs <> nexts
277+
278+ goDir :: Loc a -> (Loc a -> Maybe (Loc a)) -> List (Loc a)
279+ goDir loc' dirFn = case (dirFn loc') of
280+ Just justLoc' -> loc' : go justLoc'
281+ Nothing -> Nil
282+
267283-- Setters and Getters
268284node :: forall a. Loc a -> Tree a
269285node (Loc r) = r.node
test/Main.purs+10−2
@@ -4,10 +4,12 @@ import Prelude
44
55import Control.Comonad.Cofree (head, (:<))
66import Control.Monad.Eff (Eff)
7+import Control.Monad.Aff.Console (log)
78import Data.List (List(Nil), (:))
89import Data.Maybe (Maybe(..), fromJust)
9−import Data.Tree (Tree, mkTree, scanTree)
10−import Data.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, fromTree, insertAfter, modifyValue, next, toTree, value)
10+import Data.Tree (Tree, mkTree, scanTree, showTree)
11+import Data.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, insertAfter, modifyValue, next, toTree, value)
12+import Debug.Trace (spy)
1113import Partial.Unsafe (unsafePartial)
1214import Test.Spec (describe, it)
1315import Test.Spec.Assertions (shouldEqual)
@@ -255,5 +257,11 @@ main = run [consoleReporter] do
255257 let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
256258 shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
257259
260+ it "Should flatten the Tree into a list of locations following a depth first pattern" do
261+ let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
262+ -- log $ showTree sampleTree
263+ -- log $ show flat
264+ shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)
265+
258266
259267