This commit is contained in:
2026-04-01 11:45:25 +01:00
parent 09fc2bb48e
commit 579d5296b1
5 changed files with 62 additions and 62 deletions
+8 -6
View File
@@ -27,6 +27,7 @@ module TreeHelp (
safeUpdateSingleNode,
numTraversable,
treeAttachDeep,
subtree,
) where
import Control.Lens
@@ -49,12 +50,7 @@ treePost xs = treeFromPost (init xs) (last xs)
{- | Creates a tree with one trunk branch,
input as a list, that ends in another tree.
-}
treeFromTrunk ::
-- | The trunk
[a] ->
-- | The end of the tree
Tree a ->
Tree a
treeFromTrunk :: [a] -> Tree a -> Tree a
treeFromTrunk = flip $ foldr f
where
f x t = Node x [t]
@@ -72,6 +68,12 @@ applyToSubtree :: [Int] -> (Tree a -> Tree a) -> Tree a -> Tree a
applyToSubtree [] f t = f t
applyToSubtree (i : is) f (Node x xs) = Node x (xs & ix i %~ applyToSubtree is f)
-- the following is probably better, but hasn't been checked
subtree :: (Applicative g,Foldable f) => f Int -> (Tree b -> g (Tree b)) -> Tree b -> g (Tree b)
subtree = foldr f id
where
f i g = (branches . ix i) . g
{- | Applies a function to a specific subforest determined by a list of indices.
Partial.
-}