-- |
-- Module: Data.Tree.Optics
-- Description: Optics for working with 'Tree's.
--
-- This module defines optics for manipulating 'Tree's.
--
module Data.Tree.Optics
  ( root
  , branches
  ) where

import Data.Tree (Tree (..))

import Optics.Lens

-- | A 'Lens' that focuses on the root of a 'Tree'.
--
-- >>> view root $ Node 42 []
-- 42
root :: Lens' (Tree a) a
root :: Lens' (Tree a) a
root = LensVL (Tree a) (Tree a) a a -> Lens' (Tree a) a
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (Tree a) (Tree a) a a -> Lens' (Tree a) a)
-> LensVL (Tree a) (Tree a) a a -> Lens' (Tree a) a
forall a b. (a -> b) -> a -> b
$ \a -> f a
f (Node 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 #-}

-- | A 'Lens' returning the direct descendants of the root of a 'Tree'
--
-- @'Optics.Getter.view' 'branches' ≡ 'subForest'@
branches :: Lens' (Tree a) [Tree a]
branches :: Lens' (Tree a) [Tree a]
branches = LensVL (Tree a) (Tree a) [Tree a] [Tree a]
-> Lens' (Tree a) [Tree a]
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (Tree a) (Tree a) [Tree a] [Tree a]
 -> Lens' (Tree a) [Tree a])
-> LensVL (Tree a) (Tree a) [Tree a] [Tree a]
-> Lens' (Tree a) [Tree a]
forall a b. (a -> b) -> a -> b
$ \[Tree a] -> f [Tree a]
f (Node 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 #-}

-- $setup
-- >>> import Optics.Core