-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Views allow you to run a State monad on part of a state. -- -- A package library defining Views. @package views @version 1.0 module Data.View -- | The type definition of a view from a to v. -- -- Views allow you to operate on part (the view) of a data -- structure (the whole) while abstracting the rest. -- -- Note that while views are mostly used for operating on record fields, -- there are very interesting abstractions that may be conceived from -- them having nothing whatsoever to do with fields (for example, a view -- for the bounds of an array, that allows for easy redimensioning, or a -- view for the associated value to a given key in a map) data View a v View :: (a -> v) -> (v -> a -> a) -> View a v -- | Function to extract the view from the whole extract :: View a v -> a -> v -- | Function to reinject the view into the whole inject :: View a v -> v -> a -> a -- | A view for the first element of a pair fst_ :: View (v, b) v -- | A view for the second element of a pair snd_ :: View (a, v) v -- | A view for the head of a list head_ :: View [v] v -- | A view for the tail of a list tail_ :: View [a] [a] -- | The identity view id_ :: View a a -- | A view that encapsulates a function. -- -- Note: A View created with f_ is not a full View, as it -- doesn't allow reinjection of the view into the whole. This function is -- thus to be used only for convenience when chaining Views and pure -- functions. f_ :: (a -> v) -> View a v -- | f on v expands f to act on the whole of v. on :: (t1 -> t1) -> View t t1 -> t -> t -- | Left-to-right composition (>>>) :: Category cat => cat a b -> cat b c -> cat a c -- | Right-to-left composition (<<<) :: Category cat => cat b c -> cat a b -> cat a c instance Category View -- | A module extending the functionality of the State Monad with Views module Control.Monad.State.View -- | Constructs a State monad that acts on a View. viewState :: MonadState s m => View s t -> (t -> (a, t)) -> m a -- | Executes a state restricted to the given View. viewing :: MonadState s m => View s t -> State t a -> m a -- | Modifies the view by the given function. modifying v f is -- equivalent to viewing v (modify f). modifying :: MonadState s m => View s t -> (t -> t) -> m () -- | Gets the given view from the whole state getting :: MonadState s m => View s a -> m a -- | Injects the given value into the whole state. putting v x is -- equivalent to viewing v (put x). putting :: MonadState s m => View s t -> t -> m () -- | swappingWith v f m executes m in an environment -- where the view v was modified by f, preserving the -- old value of v (it swaps the old value and the new, and then -- swaps back after m) swappingWith :: MonadState s m => View s t -> (t -> t) -> m b -> m b -- | A special case of swappingWith with a constant value. swapping :: MonadState s m => View s t -> t -> m b -> m b -- | saving v m executes m, while preserving the value of -- the View v. saving :: MonadState s m => View s t -> m b -> m b