selections-0.1.0.3: Combinators for operating with selections over an underlying functor

Safe HaskellSafe
LanguageHaskell2010

Data.Functor.Selection

Contents

Synopsis

Selection

newtype Selection f b a Source #

The simplest selection type

Constructors

Selection 

Fields

Instances

Functor f => Bifunctor (Selection f) Source #

Bifunctor over unselected (first) and selected (second) values

Methods

bimap :: (a -> b) -> (c -> d) -> Selection f a c -> Selection f b d #

first :: (a -> b) -> Selection f a c -> Selection f b c #

second :: (b -> c) -> Selection f a b -> Selection f a c #

Traversable f => Bitraversable (Selection f) Source #

Bitraversable over unselected and selected values respectively

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Selection f a b -> f (Selection f c d) #

Foldable f => Bifoldable (Selection f) Source #

Bifoldable over unselected and selected values respectively

Methods

bifold :: Monoid m => Selection f m m -> m #

bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Selection f a b -> m #

bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Selection f a b -> c #

bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Selection f a b -> c #

Monad m => Monad (Selection m b) Source #

Selection is a monad over selected items when the underlying m is a Monad

Methods

(>>=) :: Selection m b a -> (a -> Selection m b b) -> Selection m b b #

(>>) :: Selection m b a -> Selection m b b -> Selection m b b #

return :: a -> Selection m b a #

fail :: String -> Selection m b a #

Functor f => Functor (Selection f b) Source # 

Methods

fmap :: (a -> b) -> Selection f b a -> Selection f b b #

(<$) :: a -> Selection f b b -> Selection f b a #

Monad m => Applicative (Selection m b) Source # 

Methods

pure :: a -> Selection m b a #

(<*>) :: Selection m b (a -> b) -> Selection m b a -> Selection m b b #

(*>) :: Selection m b a -> Selection m b b -> Selection m b b #

(<*) :: Selection m b a -> Selection m b b -> Selection m b a #

Foldable f => Foldable (Selection f b) Source # 

Methods

fold :: Monoid m => Selection f b m -> m #

foldMap :: Monoid m => (a -> m) -> Selection f b a -> m #

foldr :: (a -> b -> b) -> b -> Selection f b a -> b #

foldr' :: (a -> b -> b) -> b -> Selection f b a -> b #

foldl :: (b -> a -> b) -> b -> Selection f b a -> b #

foldl' :: (b -> a -> b) -> b -> Selection f b a -> b #

foldr1 :: (a -> a -> a) -> Selection f b a -> a #

foldl1 :: (a -> a -> a) -> Selection f b a -> a #

toList :: Selection f b a -> [a] #

null :: Selection f b a -> Bool #

length :: Selection f b a -> Int #

elem :: Eq a => a -> Selection f b a -> Bool #

maximum :: Ord a => Selection f b a -> a #

minimum :: Ord a => Selection f b a -> a #

sum :: Num a => Selection f b a -> a #

product :: Num a => Selection f b a -> a #

Eq (f (Either b a)) => Eq (Selection f b a) Source # 

Methods

(==) :: Selection f b a -> Selection f b a -> Bool #

(/=) :: Selection f b a -> Selection f b a -> Bool #

Show (f (Either b a)) => Show (Selection f b a) Source # 

Methods

showsPrec :: Int -> Selection f b a -> ShowS #

show :: Selection f b a -> String #

showList :: [Selection f b a] -> ShowS #

Selecting/Deselecting

Most selection combinators require that both the selected and unselected types be equal (i.e. Selection f a a); this is necessary since items will switch their selection status. Your selected and unselected types may diverge, but you'll need to unify them in order to extract your underlying functor.

newSelection :: Selectable s f => f a -> s b a Source #

Create a selection from a functor by selecting all values

forgetSelection :: Selectable s f => s a a -> f a Source #

Drops selection from your functor returning all values (selected or not).

forgetSelection . newSelection = id
forgetSelection = unify id id

select :: Selectable s f => (a -> Bool) -> s a a -> s a a Source #

Clear the selection then select only items which match a predicate.

select f = include f . deselectAll

include :: Selectable s f => (a -> Bool) -> s a a -> s a a Source #

Add items which match a predicate to the current selection

include f . select g = select (a -> f a || g a)

exclude :: Selectable s f => (a -> Bool) -> s a a -> s a a Source #

Remove items which match a predicate to the current selection

exclude f . select g = select (a -> f a && not (g a))

selectAll :: Selectable s f => s a a -> s a a Source #

Select all items in the container

selectAll = include (const True)

deselectAll :: Selectable s f => s a a -> s a a Source #

Deselect all items in the container

deselectAll = exclude (const True)

invertSelection :: Selectable s f => s b a -> s a b Source #

Flip the selection, all selected are now unselected and vice versa.

mapSelected :: Selectable s f => (a -> c) -> s b a -> s b c Source #

Map over selected values

onSelected = fmap

mapUnselected :: Selectable s f => (b -> c) -> s b a -> s c a Source #

Map over unselected values

onSelected f = modifySelection (fmap (first f))

getSelected :: (Selectable s f, Foldable f) => s b a -> [a] Source #

Collect all selected values into a list. For more complex operations use foldMap.

getSelected = foldMap (:[])

getUnselected :: (Selectable s f, Foldable f) => s b a -> [b] Source #

Collect all unselected values into a list. For more complex operations use operations from Bifoldable.

getUnselected = getSelected . invertSelection

unify :: Selectable s f => (b -> c) -> (a -> c) -> s b a -> f c Source #

Unify selected and unselected and forget the selection

unify f g == forgetSelection . onUnselected f . onSelected g

trans :: (Selectable s f, Selectable t g) => (forall c. f c -> g c) -> s b a -> t b a Source #

Perform a natural transformation over the underlying container of a selectable

Comonad Combinators

selectWithContext :: (Selectable s w, Comonad w) => (w a -> Bool) -> s a a -> s a a Source #

Select values based on their context within a comonad. This combinator makes its selection by running the predicate using extend.