{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
module Data.Tree.Lens
( root
, branches
) where
import Control.Lens
import Data.Tree
#if !MIN_VERSION_base(4,8,0)
import Data.Functor
#endif
root :: Lens' (Tree a) a
root :: (a -> f a) -> Tree a -> f (Tree a)
root f :: a -> f a
f (Node a :: a
a as :: Forest a
as) = (a -> Forest a -> Tree a
forall a. a -> Forest a -> Tree a
`Node` Forest a
as) (a -> Tree a) -> f a -> f (Tree a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
{-# INLINE root #-}
branches :: Lens' (Tree a) [Tree a]
branches :: ([Tree a] -> f [Tree a]) -> Tree a -> f (Tree a)
branches f :: [Tree a] -> f [Tree a]
f (Node a :: a
a as :: [Tree a]
as) = a -> [Tree a] -> Tree a
forall a. a -> Forest a -> Tree a
Node a
a ([Tree a] -> Tree a) -> f [Tree a] -> f (Tree a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Tree a] -> f [Tree a]
f [Tree a]
as
{-# INLINE branches #-}