a package:selective
A lifted version of
all. Retains the short-circuiting
behaviour.
Accumulate the Right values, or return the first
Left.
A lifted version of
any. Retains the short-circuiting
behaviour.
Recover the application operator
<*> from
select.
Rigid selective functors satisfy the law
<*>
= apS and furthermore, the resulting applicative
functor satisfies all laws of
Applicative:
- Identity:
pure id <*> v = v
- Homomorphism:
pure f <*> pure x = pure (f x)
- Interchange:
u <*> pure y = pure ($y) <*>
u
- Composition:
(.) <$> u <*> v <*> w = u
<*> (v <*> w)
Recover the application operator
<*> from
match.
Update a generalised sum given a generalised product that takes care
of all possible cases.
An alternative definition of applicative functors, as witnessed by
ap and
matchOne. This class is almost like
Selective but has a strict constraint on
t.
A list of values, equipped with a fast membership test.
Composition of a selective functor f and an applicative
traversable functor g.
Any applicative functor can be given a
Selective instance by
defining
select = selectA. This data type
captures this pattern, so you can use it in combination with the
DerivingVia extension as follows:
newtype Over m a = Over m
deriving (Functor, Applicative, Selective) via SelectA (Const m)
Selective instance for the standard applicative functor Validation.
This is a good example of a non-trivial selective functor which is not
a monad.
The
branch function is a natural generalisation of
select: instead of skipping an unnecessary effect, it chooses
which of the two given effectful functions to apply to a given
argument; the other effect is unnecessary. It is possible to implement
branch in terms of
select, which is a good puzzle (give
it a try!).
We can also implement
select via
branch:
selectB :: Selective f => f (Either a b) -> f (a -> b) -> f b
selectB x y = branch x y (pure id)
Embed a list of values into
Cases using the trivial but slow
membership test based on
elem.
The list of all possible values of an enumerable data type.
Eliminate all specified values a from f (Either a b)
by replacing each of them with a given f a.
Eliminate all specified values a from f (Either a b)
by replacing each of them with a given f a.
We can write a function with the type signature of
select using
the
Applicative type class, but it will always execute the
effects associated with the second argument, hence being potentially
less efficient.