Data.Filterable
- Package
- purescript-filterable
- Repository
- LiamGoodacre/purescript-filterable
#Filterable Source
class (Compactable f, Functor f) <= Filterable f whereFilterable represents data structures which can be partitioned/filtered.
partitionMap- partition a data structure based on an either predicate.partition- partition a data structure based on boolean predicate.filterMap- map over a data structure and filter based on a maybe.filter- filter a data structure based on a boolean.
Laws:
Functor Relation:
filterMap identity ≡ compactFunctor Identity:
filterMap Just ≡ identityKleisli Composition:
filterMap (l <=< r) ≡ filterMap l <<< filterMap rfilter ≡ filterMap <<< maybeBoolfilterMap p ≡ filter (isJust <<< p)Functor Relation:
partitionMap identity ≡ separateFunctor Identity 1:
_.right <<< partitionMap Right ≡ identityFunctor Identity 2:
_.left <<< partitionMap Left ≡ identityf <<< partition ≡ partitionMap <<< eitherBoolwheref = \{ no, yes } -> { left: no, right: yes }f <<< partitionMap p ≡ partition (isRight <<< p)wheref = \{ left, right } -> { no: left, yes: right}
Default implementations are provided by the following functions:
partitionDefaultpartitionDefaultFilterpartitionDefaultFilterMappartitionMapDefaultfilterDefaultfilterDefaultPartitionfilterDefaultPartitionMapfilterMapDefault
Members
partitionMap :: forall r l a. (a -> Either l r) -> f a -> { left :: f l, right :: f r }partition :: forall a. (a -> Boolean) -> f a -> { no :: f a, yes :: f a }filterMap :: forall b a. (a -> Maybe b) -> f a -> f bfilter :: forall a. (a -> Boolean) -> f a -> f a
Instances
Filterable ArrayFilterable Maybe(Monoid m) => Filterable (Either m)Filterable List(Ord k) => Filterable (Map k)
#eitherBool Source
eitherBool :: forall a. (a -> Boolean) -> a -> Either a aUpgrade a boolean-style predicate to an either-style predicate mapping.
#partitionDefault Source
partitionDefault :: forall a f. Filterable f => (a -> Boolean) -> f a -> { no :: f a, yes :: f a }A default implementation of partition using partitionMap.
#partitionDefaultFilter Source
partitionDefaultFilter :: forall a f. Filterable f => (a -> Boolean) -> f a -> { no :: f a, yes :: f a }A default implementation of partition using filter. Note that this is
almost certainly going to be suboptimal compared to direct implementations.
#partitionDefaultFilterMap Source
partitionDefaultFilterMap :: forall a f. Filterable f => (a -> Boolean) -> f a -> { no :: f a, yes :: f a }A default implementation of partition using filterMap. Note that this
is almost certainly going to be suboptimal compared to direct
implementations.
#partitionMapDefault Source
partitionMapDefault :: forall r l a f. Filterable f => (a -> Either l r) -> f a -> { left :: f l, right :: f r }A default implementation of partitionMap using separate. Note that this is
almost certainly going to be suboptimal compared to direct implementations.
#filterDefault Source
filterDefault :: forall a f. Filterable f => (a -> Boolean) -> f a -> f aA default implementation of filter using filterMap.
#filterDefaultPartition Source
filterDefaultPartition :: forall a f. Filterable f => (a -> Boolean) -> f a -> f aA default implementation of filter using partition.
#filterDefaultPartitionMap Source
filterDefaultPartitionMap :: forall a f. Filterable f => (a -> Boolean) -> f a -> f aA default implementation of filter using partitionMap.
#filterMapDefault Source
filterMapDefault :: forall b a f. Filterable f => (a -> Maybe b) -> f a -> f bA default implementation of filterMap using separate. Note that this is
almost certainly going to be suboptimal compared to direct implementations.
#cleared Source
cleared :: forall b a f. Filterable f => f a -> f bFilter out all values.
Re-exports from Data.Compactable
#Compactable Source
class Compactable f whereCompactable 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 <*> _) ≡ idapplyMaybe (pure Just) ≡ idcompact ≡ applyMaybe (pure id)
If the data type is also a Monad the following should hold:
flip bindMaybe (pure <<< Just) ≡ idcompact <<< (pure <<< (Just (=<<))) ≡ idcompact ≡ flip bindMaybe pure
If the data type is also Plus the following should hold:
compact empty ≡ emptycompact (const Nothing <$> xs) ≡ empty
Members
compact :: forall a. f (Maybe a) -> f aseparate :: forall r l. f (Either l r) -> { left :: f l, right :: f r }
Instances
Compactable Maybe(Monoid m) => Compactable (Either m)Compactable ArrayCompactable List(Ord k) => Compactable (Map k)