polytree 0.0.4 → 0.0.5
raw patch · 3 files changed
+70/−16 lines, 3 files
Files
- changelog.md +5/−0
- polytree.cabal +1/−1
- src/Data/PolyTree.hs +64/−15
changelog.md view
@@ -1,3 +1,8 @@+0.0.5++* Add `nodeA` constructor function+* Rearrange and rename fold functions, adding `foldTreeM`+ 0.0.4 * Add `nodeValue` and `nodeChildren`
polytree.cabal view
@@ -1,5 +1,5 @@ name: polytree-version: 0.0.4+version: 0.0.5 synopsis: A polymorphic rose-tree description: A rose-tree which has different data in the nodes and leaves license: BSD3
src/Data/PolyTree.hs view
@@ -4,7 +4,7 @@ module Data.PolyTree where -import Control.Applicative ( Applicative(liftA2) )+import Control.Applicative ( Applicative(liftA2), Alternative(empty) ) import Control.Lens ( preview, iso,@@ -367,7 +367,7 @@ -- >>> toListOf plate (Node 1 [Leaf 2] :: Tree0' Int) -- [Leaf 2] ----- >>> > toListOf plate (Node 1 [Leaf 2, Leaf 3, Node 4 []] :: Tree0' Int)+-- >>> toListOf plate (Node 1 [Leaf 2, Leaf 3, Node 4 []] :: Tree0' Int) -- [Leaf 2,Leaf 3,Node 4 []] -- -- >>> toListOf plate (Node 1 [Leaf 2, Leaf 3, Node 4 [Leaf 5]] :: Tree0' Int)@@ -378,28 +378,70 @@ plate f (Node a ts) = Node a <$> traverse f ts -foldTree ::+matchTree :: (b -> x) -> (a -> f (Tree f a b) -> x) -> Tree f a b -> x-foldTree l _ (Leaf b) =+matchTree l _ (Leaf b) = l b-foldTree _ n (Node a t) =+matchTree _ n (Node a t) = n a t -foldTreeFix ::+foldTree :: Functor f => (b -> x) -> (a -> f x -> x) -> Tree f a b -> x-foldTreeFix l _ (Leaf b) =+foldTree l _ (Leaf b) = l b-foldTreeFix l n (Node a t) =- n a (fmap (foldTreeFix l n) t)+foldTree l n (Node a t) =+ n a (fmap (foldTree l n) t) -- | --+-- >>> foldTreeM (\b -> [b, b]) (:) (Leaf 1)+-- [1,1]+--+-- >>> foldTreeM (\b -> [b, b]) (:) (Leaf 1)+-- [1,1]+--+-- >>> foldTreeM (\b -> [b, b]) (:) (Node 1 [Leaf 2])+-- [1,2,1,2]+--+-- >>> foldTreeM (\b -> [b, b]) (:) (Node 1 [Leaf 2, Leaf 3])+-- [1,2,3,1,2,3,1,2,3,1,2,3]+--+-- >>> foldTreeM (\b -> [b, b]) (:) (Node 1 [Leaf 2, Leaf 3, Node 4 []])+-- [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]+--+-- >>> foldTreeM (\b -> [b, b * 10]) (:) (Leaf 1)+-- [1,10]+--+-- >>> foldTreeM (\b -> [b, b * 10]) (:) (Leaf 1)+-- [1,10]+--+-- >>> foldTreeM (\b -> [b, b * 10]) (:) (Node 1 [Leaf 2])+-- [1,2,1,20]+--+-- >>> foldTreeM (\b -> [b, b * 10]) (:) (Node 1 [Leaf 2, Leaf 3])+-- [1,2,3,1,2,30,1,20,3,1,20,30]+--+-- >>> foldTreeM (\b -> [b, b * 10]) (:) (Node 1 [Leaf 2, Leaf 3, Node 4 []])+-- [1,2,3,4,1,2,30,4,1,20,3,4,1,20,30,4]+foldTreeM ::+ (Monad m, Traversable f) =>+ (b -> m x)+ -> (a -> f x -> m x)+ -> Tree f a b+ -> m x+foldTreeM l _ (Leaf b) =+ l b+foldTreeM l n (Node a t) =+ traverse (foldTreeM l n) t >>= n a++-- |+-- -- >>> treeValue (Leaf 1 :: Tree0' Int) -- Right 1 --@@ -415,7 +457,7 @@ Tree f a b -> Either a b treeValue =- foldTree Right (pure . Left)+ matchTree Right (pure . Left) -- | --@@ -434,7 +476,7 @@ Tree f a b -> Either b (f (Tree f a b)) treeChildren =- foldTree Left (pure Right)+ matchTree Left (pure Right) -- | --@@ -605,6 +647,13 @@ Left (Leaf b) ) +nodeA ::+ Alternative f =>+ a+ -> Tree f a b+nodeA a =+ Node a empty+ node :: a -> f (Tree f a b)@@ -647,7 +696,7 @@ (a', Maybe (f' (Tree' f' a'))) treeIso = iso- (foldTree (, Nothing) (\a t -> (a, Just t)))+ (matchTree (, Nothing) (\a t -> (a, Just t))) (\(a, t) -> maybe (Leaf a) (Node a) t) -- |@@ -704,7 +753,7 @@ (Free (Compose ((,) a') f') b') treeFree = iso- (foldTreeFix Pure (\a t -> Free (Compose (a, t))))+ (foldTree Pure (\a t -> Free (Compose (a, t)))) ( let go (Pure b) = Leaf b@@ -723,7 +772,7 @@ (FreeT.Free (Compose ((,) a') f') b') treeFreeT = iso- (foldTreeFix (FreeT.free . FreeT.Pure) (\a t -> FreeT.free (FreeT.Free (Compose (a, t)))))+ (foldTree (FreeT.free . FreeT.Pure) (\a t -> FreeT.free (FreeT.Free (Compose (a, t))))) ( let go (FreeT.Pure b) = Leaf b@@ -764,5 +813,5 @@ in Tree.Node h (fmap go c) mkTree0 (Tree.Node h t) = Node h (fmap mkTree0 t) in iso- (foldTree absurd mkTree)+ (matchTree absurd mkTree) (\(Tree.Node h t) -> Node h (fmap mkTree0 t))