module Data.Functor.Context (
Ctxt(..),
toCtxt,
mapCtxt,
extractCtxt,
addCtxt,
) where
import Control.Comonad
import Control.Applicative
import Control.Lens.Wrapped
import Control.Lens.Iso
newtype Ctxt a = Ctxt { getCtxt :: (Maybe a, a, Maybe a) }
deriving (Functor, Eq, Ord)
instance Show a => Show (Ctxt a) where
show (Ctxt vs) = "toCtxt " ++ show vs
instance Wrapped (Ctxt a) where
type Unwrapped (Ctxt a) = (Maybe a, a, Maybe a)
_Wrapped' = iso getCtxt Ctxt
instance Rewrapped (Ctxt a) (Ctxt b)
instance Applicative Ctxt where
pure x = Ctxt (Nothing, x, Nothing)
Ctxt (b,x,a) <*> Ctxt (b',x',a') = Ctxt (b <*> b', x x', a <*> a')
toCtxt = Ctxt
mapCtxt :: (a -> b) -> Ctxt a -> Ctxt b
mapCtxt = fmap
extractCtxt :: Ctxt a -> a
extractCtxt (Ctxt (_,x,_)) = x
addCtxt :: [a] -> [Ctxt a]
addCtxt = fmap Ctxt . withPrevNext
where
withPrevNext :: [a] -> [(Maybe a, a, Maybe a)]
withPrevNext xs = zip3 (pure Nothing ++ fmap Just xs) xs (fmap Just (tail xs) ++ repeat Nothing)