focus-1.0.3: A general abstraction for manipulating elements of container data structures
Safe HaskellNone
LanguageHaskell2010

Focus

Synopsis

Documentation

data Focus element m result Source #

Abstraction over the modification of an element of a datastructure.

It is composable using the standard typeclasses, e.g.:

lookupAndDelete :: Monad m => Focus a m (Maybe a)
lookupAndDelete = lookup <* delete

Constructors

Focus (m (result, Change element)) (element -> m (result, Change element)) 

Instances

Instances details
MonadTrans (Focus element) Source # 
Instance details

Defined in Focus

Methods

lift :: Monad m => m a -> Focus element m a Source #

Monad m => Monad (Focus element m) Source # 
Instance details

Defined in Focus

Methods

(>>=) :: Focus element m a -> (a -> Focus element m b) -> Focus element m b Source #

(>>) :: Focus element m a -> Focus element m b -> Focus element m b Source #

return :: a -> Focus element m a Source #

Functor m => Functor (Focus element m) Source # 
Instance details

Defined in Focus

Methods

fmap :: (a -> b) -> Focus element m a -> Focus element m b Source #

(<$) :: a -> Focus element m b -> Focus element m a Source #

Monad m => Applicative (Focus element m) Source # 
Instance details

Defined in Focus

Methods

pure :: a -> Focus element m a Source #

(<*>) :: Focus element m (a -> b) -> Focus element m a -> Focus element m b Source #

liftA2 :: (a -> b -> c) -> Focus element m a -> Focus element m b -> Focus element m c Source #

(*>) :: Focus element m a -> Focus element m b -> Focus element m b Source #

(<*) :: Focus element m a -> Focus element m b -> Focus element m a Source #

data Change a Source #

What to do with the focused value.

The interpretation of the commands is up to the context APIs.

Constructors

Leave

Produce no changes

Remove

Delete it

Set a

Set its value to the provided one

Instances

Instances details
Functor Change Source # 
Instance details

Defined in Focus

Methods

fmap :: (a -> b) -> Change a -> Change b Source #

(<$) :: a -> Change b -> Change a Source #

Eq a => Eq (Change a) Source # 
Instance details

Defined in Focus

Methods

(==) :: Change a -> Change a -> Bool Source #

(/=) :: Change a -> Change a -> Bool Source #

Ord a => Ord (Change a) Source # 
Instance details

Defined in Focus

Methods

compare :: Change a -> Change a -> Ordering Source #

(<) :: Change a -> Change a -> Bool Source #

(<=) :: Change a -> Change a -> Bool Source #

(>) :: Change a -> Change a -> Bool Source #

(>=) :: Change a -> Change a -> Bool Source #

max :: Change a -> Change a -> Change a Source #

min :: Change a -> Change a -> Change a Source #

Show a => Show (Change a) Source # 
Instance details

Defined in Focus

Semigroup (Change a) Source # 
Instance details

Defined in Focus

Methods

(<>) :: Change a -> Change a -> Change a Source #

sconcat :: NonEmpty (Change a) -> Change a Source #

stimes :: Integral b => b -> Change a -> Change a Source #

Monoid (Change a) Source # 
Instance details

Defined in Focus

Pure functions

Reading functions

member :: Monad m => Focus a m Bool Source #

Reproduces the behaviour of Data.Map.member.

lookup :: Monad m => Focus a m (Maybe a) Source #

Reproduces the behaviour of Data.Map.lookup.

lookupWithDefault :: Monad m => a -> Focus a m a Source #

Reproduces the behaviour of Data.Map.findWithDefault with a better name.

Modifying functions

delete :: Monad m => Focus a m () Source #

Reproduces the behaviour of Data.Map.delete.

lookupAndDelete :: Monad m => Focus a m (Maybe a) Source #

Lookup an element and delete it if it exists.

Same as lookup <* delete.

insert :: Monad m => a -> Focus a m () Source #

Reproduces the behaviour of Data.Map.insert.

insertOrMerge :: Monad m => (a -> a -> a) -> a -> Focus a m () Source #

Reproduces the behaviour of Data.Map.insertWith with a better name.

alter :: Monad m => (Maybe a -> Maybe a) -> Focus a m () Source #

Reproduces the behaviour of Data.Map.alter.

adjust :: Monad m => (a -> a) -> Focus a m () Source #

Reproduces the behaviour of Data.Map.adjust.

update :: Monad m => (a -> Maybe a) -> Focus a m () Source #

Reproduces the behaviour of Data.Map.update.

accessAndAdjust :: Monad m => (s -> a) -> (s -> s) -> Focus s m (Maybe a) Source #

Same as all of the following expressions:

f g -> fmap (fmap f) lookup <* adjust g f g -> liftStateFn (f &&& g) f g -> liftStateFn ((,) $ f * g)

liftState :: Monad m => State s a -> Focus s m (Maybe a) Source #

Lift a pure state monad.

liftStateFn :: Monad m => (s -> (a, s)) -> Focus s m (Maybe a) Source #

Lift a pure state-monad-like function.

Construction utils

cases :: Monad m => (b, Change a) -> (a -> (b, Change a)) -> Focus a m b Source #

Lift pure functions which handle the cases of presence and absence of the element.

unitCases :: Monad m => Change a -> (a -> Change a) -> Focus a m () Source #

Lift pure functions which handle the cases of presence and absence of the element and produce no result.

Monadic functions

Reading functions

lookupWithDefaultM :: Monad m => m a -> Focus a m a Source #

A monadic version of lookupWithDefault.

Modifying functions

insertM :: Monad m => m a -> Focus a m () Source #

A monadic version of insert.

insertOrMergeM :: Monad m => (a -> a -> m a) -> m a -> Focus a m () Source #

A monadic version of insertOrMerge.

alterM :: Monad m => (Maybe a -> m (Maybe a)) -> Focus a m () Source #

A monadic version of alter.

adjustM :: Monad m => (a -> m a) -> Focus a m () Source #

A monadic version of adjust.

updateM :: Monad m => (a -> m (Maybe a)) -> Focus a m () Source #

A monadic version of update.

Construction utils

casesM :: Monad m => m (b, Change a) -> (a -> m (b, Change a)) -> Focus a m b Source #

Lift monadic functions which handle the cases of presence and absence of the element.

unitCasesM :: Monad m => m (Change a) -> (a -> m (Change a)) -> Focus a m () Source #

Lift monadic functions which handle the cases of presence and absence of the element and produce no result.

Composition

mappingInput :: Monad m => (a -> b) -> (b -> a) -> Focus a m x -> Focus b m x Source #

Map the Focus input.

Change-inspecting functions

extractingInput :: Monad m => Focus a m b -> Focus a m (b, Maybe a) Source #

Extends the output with the input.

extractingChange :: Monad m => Focus a m b -> Focus a m (b, Change a) Source #

Extends the output with the change performed.

projectingChange :: Monad m => (Change a -> c) -> Focus a m b -> Focus a m (b, c) Source #

Extends the output with a projection on the change that was performed.

testingIfModifies :: Monad m => Focus a m b -> Focus a m (b, Bool) Source #

Extends the output with a flag, signaling whether a change, which is not Leave, has been introduced.

testingIfRemoves :: Monad m => Focus a m b -> Focus a m (b, Bool) Source #

Extends the output with a flag, signaling whether the Remove change has been introduced.

testingIfInserts :: Monad m => Focus a m b -> Focus a m (b, Bool) Source #

Extends the output with a flag, signaling whether an item will be inserted. That is, it didn't exist before and a Set change is introduced.

testingSizeChange Source #

Arguments

:: Monad m 
=> sizeChange

Decreased

-> sizeChange

Didn't change

-> sizeChange

Increased

-> Focus a m b 
-> Focus a m (b, sizeChange) 

Extend the output with a flag, signaling how the size will be affected by the change.

STM

onTVarValue :: Focus a STM b -> Focus (TVar a) STM b Source #

Focus on the contents of a TVar.