Data.Compactable
- Package
- purescript-filterable
- Repository
- LiamGoodacre/purescript-filterable
#Compactable Source
class Compactable f where
Compactable
represents data structures which can be compacted/filtered.
This is a generalization of catMaybes as a new function compact
. compact
has relations with Functor
, Applicative
, Monad
, Plus
, and Traversable
in that we can use these classes to provide the ability to operate on a data type
by eliminating intermediate Nothings. This is useful for representing the
filtering out of values, or failure.
To be compactable alone, no laws must be satisfied other than the type signature.
If the data type is also a Functor the following should hold:
- Functor Identity:
compact <<< map Just ≡ id
According to Kmett, (Compactable f, Functor f) is a functor from the
kleisli category of Maybe to the category of Hask.
Kleisli Maybe -> Hask
.
If the data type is also Applicative
the following should hold:
compact <<< (pure Just <*> _) ≡ id
applyMaybe (pure Just) ≡ id
compact ≡ applyMaybe (pure id)
If the data type is also a Monad
the following should hold:
flip bindMaybe (pure <<< Just) ≡ id
compact <<< (pure <<< (Just (=<<))) ≡ id
compact ≡ flip bindMaybe pure
If the data type is also Plus
the following should hold:
compact empty ≡ empty
compact (const Nothing <$> xs) ≡ empty
Members
compact :: forall a. f (Maybe a) -> f a
separate :: forall r l. f (Either l r) -> { left :: f l, right :: f r }
Instances
Compactable Maybe
(Monoid m) => Compactable (Either m)
Compactable Array
Compactable List
(Ord k) => Compactable (Map k)
#compactDefault Source
compactDefault :: forall a f. Functor f => Compactable f => f (Maybe a) -> f a
#separateDefault Source
separateDefault :: forall r l f. Functor f => Compactable f => f (Either l r) -> { left :: f l, right :: f r }
#applyMaybe Source
applyMaybe :: forall b a f. Apply f => Compactable f => f (a -> Maybe b) -> f a -> f b
#applyEither Source
applyEither :: forall r l a f. Apply f => Compactable f => f (a -> Either l r) -> f a -> { left :: f l, right :: f r }
#bindMaybe Source
bindMaybe :: forall b a m. Bind m => Compactable m => m a -> (a -> m (Maybe b)) -> m b
#bindEither Source
bindEither :: forall r l a m. Bind m => Compactable m => m a -> (a -> m (Either l r)) -> { left :: m l, right :: m r }