Implement transitivity instance, extract a part of modifyAt as a convenient 'edit' function useful elsewhere and present a right-infix version of (,) to allow writing the nested tuple indexes more conveniently
This commit is contained in:
parent
a9252b129a
commit
e607f9cd37
1 changed files with 14 additions and 5 deletions
|
@ -5,19 +5,28 @@
|
||||||
{-# LANGUAGE UndecidableInstances #-}
|
{-# LANGUAGE UndecidableInstances #-}
|
||||||
module PDF.Box (
|
module PDF.Box (
|
||||||
Box(..)
|
Box(..)
|
||||||
|
, (.@)
|
||||||
|
, edit
|
||||||
, modifyAt
|
, modifyAt
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad.State (MonadState(..))
|
import Control.Monad.State (MonadState(..))
|
||||||
|
|
||||||
class Box m i a b | m a i -> b where
|
class Monad m => Box m i a b | m a i -> b where
|
||||||
r :: i -> a -> m b
|
r :: i -> a -> m b
|
||||||
w :: i -> a -> b -> m a
|
w :: i -> a -> b -> m a
|
||||||
|
|
||||||
|
edit :: Box m i a b => i -> (b -> m b) -> a -> m a
|
||||||
|
edit i f a =
|
||||||
|
r i a >>= f >>= w i a
|
||||||
|
|
||||||
modifyAt :: (MonadState a m, Box m i a b) => i -> (b -> m b) -> m ()
|
modifyAt :: (MonadState a m, Box m i a b) => i -> (b -> m b) -> m ()
|
||||||
modifyAt i f = do
|
modifyAt i f = get >>= edit i f >>= put
|
||||||
a <- get
|
|
||||||
r i a >>= f >>= w i a >>= put
|
|
||||||
|
|
||||||
instance (Box m i a b, Box m j b c) => Box m (i, j) a c
|
instance (Box m i a b, Box m j b c) => Box m (i, j) a c where
|
||||||
|
r (i, j) a = r i a >>= r j
|
||||||
|
w (i, j) a c = r i a >>= flip (w j) c >>= w i a
|
||||||
|
|
||||||
|
infixr 6 .@
|
||||||
|
(.@) :: a -> b -> (a, b)
|
||||||
|
(.@) = (,)
|
||||||
|
|
Loading…
Reference in a new issue