Data.Filterable
- Package
- purescript-filterable
- Repository
- LiamGoodacre/purescript-filterable
#Filterable Source
class (Compactable f, Functor f) <= Filterable f where
Filterable
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 ≡ compact
Functor Identity:
filterMap Just ≡ identity
Kleisli Composition:
filterMap (l <=< r) ≡ filterMap l <<< filterMap r
filter ≡ filterMap <<< maybeBool
filterMap p ≡ filter (isJust <<< p)
Functor Relation:
partitionMap identity ≡ separate
Functor Identity 1:
_.right <<< partitionMap Right ≡ identity
Functor Identity 2:
_.left <<< partitionMap Left ≡ identity
f <<< partition ≡ partitionMap <<< eitherBool
wheref = \{ 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:
partitionDefault
partitionDefaultFilter
partitionDefaultFilterMap
partitionMapDefault
filterDefault
filterDefaultPartition
filterDefaultPartitionMap
filterMapDefault
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 b
filter :: forall a. (a -> Boolean) -> f a -> f a
Instances
Filterable Array
Filterable Maybe
(Monoid m) => Filterable (Either m)
Filterable List
(Ord k) => Filterable (Map k)
#eitherBool Source
eitherBool :: forall a. (a -> Boolean) -> a -> Either a a
Upgrade 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 a
A default implementation of filter
using filterMap
.
#filterDefaultPartition Source
filterDefaultPartition :: forall a f. Filterable f => (a -> Boolean) -> f a -> f a
A default implementation of filter
using partition
.
#filterDefaultPartitionMap Source
filterDefaultPartitionMap :: forall a f. Filterable f => (a -> Boolean) -> f a -> f a
A default implementation of filter
using partitionMap
.
#filterMapDefault Source
filterMapDefault :: forall b a f. Filterable f => (a -> Maybe b) -> f a -> f b
A 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 b
Filter out all values.
Re-exports from Data.Compactable
#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)