module BishBosh.Property.Tree(
Prunable(..),
Depth
) where
import qualified Data.Tree
type Depth = Int
class Prunable tree where
prune :: Depth -> tree -> tree
instance Prunable (Data.Tree.Tree a) where
prune :: Depth -> Tree a -> Tree a
prune Depth
depth tree :: Tree a
tree@Data.Tree.Node { subForest :: forall a. Tree a -> Forest a
Data.Tree.subForest = Forest a
forest }
| Depth
depth Depth -> Depth -> Bool
forall a. Ord a => a -> a -> Bool
< Depth
0 = [Char] -> Tree a
forall a. HasCallStack => [Char] -> a
error [Char]
"BishBosh.Property.Tree.prune:\tdepth can't be negative."
| Bool
otherwise = Tree a
tree {
subForest :: Forest a
Data.Tree.subForest = if Depth
depth Depth -> Depth -> Bool
forall a. Eq a => a -> a -> Bool
== Depth
0
then []
else (Tree a -> Tree a) -> Forest a -> Forest a
forall a b. (a -> b) -> [a] -> [b]
map (
Depth -> Tree a -> Tree a
forall tree. Prunable tree => Depth -> tree -> tree
prune (Depth -> Tree a -> Tree a) -> Depth -> Tree a -> Tree a
forall a b. (a -> b) -> a -> b
$ Depth -> Depth
forall a. Enum a => a -> a
pred Depth
depth
) Forest a
forest
}