Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Acme.Functors
Description
Types are great. Lifting them into some sort of applicative functor makes them even better. This module is an homage to our favorite applicatives, and to the semigroups with which they are instrinsically connected.
Synopsis
- data LiftedButWhy a = LiftedButWhy a
- data OrNot a
- = ActuallyYes a
- | Nope
- data Two a = Two {
- firstOfTwo :: a
- secondOfTwo :: a
- data AnyNumberOf a
- = OneAndMaybeMore a (AnyNumberOf a)
- | ActuallyNone
- (~~) :: a -> AnyNumberOf a -> AnyNumberOf a
- data OneOrMore a = OneOrMore {
- theFirstOfMany :: a
- possiblyMore :: AnyNumberOf a
- data Also extraThing a = Also {
- withoutExtraThing :: a
- theExtraThing :: extraThing
- data OrInstead otherThing a
- = NotInstead a
- | Instead otherThing
- data OrInsteadFirst otherThing a
- = NotInsteadFirst a
- | InsteadFirst otherThing
- data DeterminedBy parameter a = Determination ((->) parameter a)
Lifted-but-why
data LiftedButWhy a Source #
LiftedButWhy
is a boring functor that just has one value and no other
structure or interesting properties.
Constructors
LiftedButWhy a | A value that has been lifted for some damned reason. ... Okay, to be honest, this one is nobody's favorite, but it is included here for completeness. |
Instances
Or-not
OrNot
is somehow slightly more interesting than LiftedButWhy
, even
though it may actually contain less. Instead of a value, there might not
be a value.
When you combine stuff with (<*>)
or (<>)
, all of the values need to
be present. If any of them are absent, the whole expression evaluates to
Nope
.
Constructors
ActuallyYes a | Some normal value. |
Nope | Chuck Testa. |
Instances
Monad OrNot Source # | |
Functor OrNot Source # | |
Applicative OrNot Source # | If you have a function pure = ActuallyYes ActuallyYes f <*> ActuallyYes a = ActuallyYes (f a) _ <*> _ = Nope |
Eq a => Eq (OrNot a) Source # | |
Show a => Show (OrNot a) Source # | |
Semigroup a => Semigroup (OrNot a) Source # | If you have value ActuallyYes a <> ActuallyYes a' = ActuallyYes (a <> a') _ <> _ = Nope |
Monoid a => Monoid (OrNot a) Source # | mempty = ActuallyYes mempty |
Defined in Acme.Functors |
Two
Two
is two values. Yep. Just two values.
Constructors
Two | |
Fields
|
Instances
Functor Two Source # | |
Applicative Two Source # | If you have two functions pure a = Two a a Two f g <*> Two a a' = Two (f a) (g a') |
Eq a => Eq (Two a) Source # | |
Show a => Show (Two a) Source # | |
Semigroup a => Semigroup (Two a) Source # | Two x y <> Two x' y' = Two (x <> x') (y <> y') |
Monoid a => Monoid (Two a) Source # | mempty = Two mempty mempty |
Defined in Acme.Functors |
Any-number-of
data AnyNumberOf a Source #
AnyNumberOf
starts to get exciting. Any number of values you want.
Zero ... one ... two ... three ... four ... five ... The possibilities are
truly endless.
Constructors
OneAndMaybeMore a (AnyNumberOf a) | One value, and maybe even more after that! |
ActuallyNone | Oh. Well this is less fun. |
Instances
Functor AnyNumberOf Source # | |
Defined in Acme.Functors Methods fmap :: (a -> b) -> AnyNumberOf a -> AnyNumberOf b # (<$) :: a -> AnyNumberOf b -> AnyNumberOf a # | |
Applicative AnyNumberOf Source # | You can use this to apply any number of functions to any number of arguments. pure a = OneAndMaybeMore a ActuallyNone OneAndMaybeMore f fs <*> OneAndMaybeMore x xs = OneAndMaybeMore (f x) (fs <*> xs) _ <*> _ = ActuallyNone Example: ( (+ 1) ~~ (* 2) ~~ (+ 5) ~~ ActuallyNone ) <*> ( 1 ~~ 6 ~~ 4 ~~ 37 ~~ ActuallyNone ) = ( 2 ~~ 12 ~~ 9 ~~ ActuallyNone ) This example demonstrates how when there are more arguments than functions,
any excess arguments (in this case, the |
Defined in Acme.Functors Methods pure :: a -> AnyNumberOf a # (<*>) :: AnyNumberOf (a -> b) -> AnyNumberOf a -> AnyNumberOf b # liftA2 :: (a -> b -> c) -> AnyNumberOf a -> AnyNumberOf b -> AnyNumberOf c # (*>) :: AnyNumberOf a -> AnyNumberOf b -> AnyNumberOf b # (<*) :: AnyNumberOf a -> AnyNumberOf b -> AnyNumberOf a # | |
Eq a => Eq (AnyNumberOf a) Source # | |
Defined in Acme.Functors Methods (==) :: AnyNumberOf a -> AnyNumberOf a -> Bool # (/=) :: AnyNumberOf a -> AnyNumberOf a -> Bool # | |
Show a => Show (AnyNumberOf a) Source # | |
Defined in Acme.Functors Methods showsPrec :: Int -> AnyNumberOf a -> ShowS # show :: AnyNumberOf a -> String # showList :: [AnyNumberOf a] -> ShowS # | |
Semigroup a => Semigroup (AnyNumberOf a) Source # | The operation of combining some number of OneAndMaybeMore x xs <> OneAndMaybeMore y ys = OneAndMaybeMore (x <> y) (xs <> ys) _ <> _ = ActuallyNone |
Defined in Acme.Functors Methods (<>) :: AnyNumberOf a -> AnyNumberOf a -> AnyNumberOf a # sconcat :: NonEmpty (AnyNumberOf a) -> AnyNumberOf a # stimes :: Integral b => b -> AnyNumberOf a -> AnyNumberOf a # | |
Monoid a => Monoid (AnyNumberOf a) Source # | mempty = mempty ~~ mempty |
Defined in Acme.Functors Methods mempty :: AnyNumberOf a Source # |
(~~) :: a -> AnyNumberOf a -> AnyNumberOf a infixr 5 Source #
Alias for OneAndMaybeMore
which provides some brevity.
One-or-more
OneOrMore
is more restrictive than AnyNumberOf
, yet somehow
actually more interesting, because it excludes that dull situation where
there aren't any values at all.
Constructors
OneOrMore | |
Fields
|
Instances
Functor OneOrMore Source # | |
Applicative OneOrMore Source # | pure a = OneOrMore a ActuallyNone OneOrMore f fs <*> OneOrMore x xs = OneOrMore (f x) (fs <*> xs) |
Eq a => Eq (OneOrMore a) Source # | |
Show a => Show (OneOrMore a) Source # | |
Semigroup a => Semigroup (OneOrMore a) Source # | OneOrMore a more <> OneOrMore a' more' = OneOrMore a (more <> OneAndMaybeMore a' more') |
Monoid a => Monoid (OneOrMore a) Source # | mempty = OneOrMore mempty ActuallyNone |
Defined in Acme.Functors |
Also-extra-thing
data Also extraThing a Source #
Also extraThing
is a functor in which each value has an extraThing
of some other type that tags along with it.
Constructors
Also | |
Fields
|
Instances
Functor (Also extraThing) Source # | |
Monoid extraThing => Applicative (Also extraThing) Source # | Dragging the pure = (`Also` mempty) (f `Also` extra1) <*> (a `Also` extra2) = f a `Also` (extra1 <> extra2) |
Defined in Acme.Functors Methods pure :: a -> Also extraThing a # (<*>) :: Also extraThing (a -> b) -> Also extraThing a -> Also extraThing b # liftA2 :: (a -> b -> c) -> Also extraThing a -> Also extraThing b -> Also extraThing c # (*>) :: Also extraThing a -> Also extraThing b -> Also extraThing b # (<*) :: Also extraThing a -> Also extraThing b -> Also extraThing a # | |
(Eq a, Eq extraThing) => Eq (Also extraThing a) Source # | |
(Show a, Show extraThing) => Show (Also extraThing a) Source # | |
(Semigroup extraThing, Semigroup a) => Semigroup (Also extraThing a) Source # | (a `Also` extra1) <> (a' `Also` extra2) = (a <> a') `Also` (extra1 <> extra2) |
(Monoid extraThing, Monoid a) => Monoid (Also extraThing a) Source # | mempty = Also mempty mempty |
Defined in Acme.Functors |
Or-instead-other-thing
data OrInstead otherThing a Source #
OrInstead otherThing
is a functor in which, instead of having a
value, can actually just have some totally unrelated otherThing
instead.
When you combine stuff with (<*>)
or (<>)
, all of the values need to
be present. If any of them are the otherThing
instead, then the whole
expression evaluates to the combination of the otherThing
s.
Constructors
NotInstead a | A normal value. |
Instead otherThing | Some totally unrelated other thing. |
Instances
Functor (OrInstead otherThing) Source # | |
Semigroup otherThing => Applicative (OrInstead otherThing) Source # | The possibility of having an pure = NotInstead NotInstead f <*> NotInstead a = NotInstead (f a) Instead other1 <*> Instead other2 = Instead (other1 <> other2) Instead other <*> _ = Instead other _ <*> Instead other = Instead other |
Defined in Acme.Functors Methods pure :: a -> OrInstead otherThing a # (<*>) :: OrInstead otherThing (a -> b) -> OrInstead otherThing a -> OrInstead otherThing b # liftA2 :: (a -> b -> c) -> OrInstead otherThing a -> OrInstead otherThing b -> OrInstead otherThing c # (*>) :: OrInstead otherThing a -> OrInstead otherThing b -> OrInstead otherThing b # (<*) :: OrInstead otherThing a -> OrInstead otherThing b -> OrInstead otherThing a # | |
(Eq a, Eq otherThing) => Eq (OrInstead otherThing a) Source # | |
(Show a, Show otherThing) => Show (OrInstead otherThing a) Source # | |
(Semigroup otherThing, Semigroup a) => Semigroup (OrInstead otherThing a) Source # | NotInstead a <> NotInstead a' = NotInstead (a <> a') Instead other1 <> Instead other2 = Instead (other1 <> other2) Instead other <> _ = Instead other _ <> Instead other = Instead other |
(Semigroup otherThing, Monoid a) => Monoid (OrInstead otherThing a) Source # | |
Defined in Acme.Functors |
Or-instead-other-thing ("first" variant)
data OrInsteadFirst otherThing a Source #
OrInsteadFirst otherThing
looks a lot like OrInstead otherThing
,
but it manages to always be an applicative functor — and even a monad too —
by handling the otherThing
s a bit more hamfistedly.
When you combine stuff with (<*>)
or (<>)
, all of the values need to
be present. If any of them are the otherThing
instead, then the whole
expression evaluates to the first otherThing
encountered, ignoring any
additional otherThing
s that may subsequently pop up.
Constructors
NotInsteadFirst a | A normal value. |
InsteadFirst otherThing | Some totally unrelated other thing. |
Instances
Monad (OrInsteadFirst otherThing) Source # | InsteadFirst other >>= _ = InsteadFirst other NotInsteadFirst a >>= f = f a |
Defined in Acme.Functors Methods (>>=) :: OrInsteadFirst otherThing a -> (a -> OrInsteadFirst otherThing b) -> OrInsteadFirst otherThing b # (>>) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b -> OrInsteadFirst otherThing b # return :: a -> OrInsteadFirst otherThing a # | |
Functor (OrInsteadFirst otherThing) Source # | |
Defined in Acme.Functors Methods fmap :: (a -> b) -> OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b # (<$) :: a -> OrInsteadFirst otherThing b -> OrInsteadFirst otherThing a # | |
Applicative (OrInsteadFirst otherThing) Source # | pure = NotInsteadFirst NotInsteadFirst f <*> NotInsteadFirst a = NotInsteadFirst (f a) InsteadFirst other <*> _ = InsteadFirst other _ <*> InsteadFirst other = InsteadFirst other |
Defined in Acme.Functors Methods pure :: a -> OrInsteadFirst otherThing a # (<*>) :: OrInsteadFirst otherThing (a -> b) -> OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b # liftA2 :: (a -> b -> c) -> OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b -> OrInsteadFirst otherThing c # (*>) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b -> OrInsteadFirst otherThing b # (<*) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing b -> OrInsteadFirst otherThing a # | |
(Eq a, Eq otherThing) => Eq (OrInsteadFirst otherThing a) Source # | |
Defined in Acme.Functors Methods (==) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing a -> Bool # (/=) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing a -> Bool # | |
(Show a, Show otherThing) => Show (OrInsteadFirst otherThing a) Source # | |
Defined in Acme.Functors Methods showsPrec :: Int -> OrInsteadFirst otherThing a -> ShowS # show :: OrInsteadFirst otherThing a -> String # showList :: [OrInsteadFirst otherThing a] -> ShowS # | |
(Semigroup otherThing, Semigroup a) => Semigroup (OrInsteadFirst otherThing a) Source # | NotInsteadFirst a <> NotInsteadFirst a' = NotInsteadFirst (a <> a') InsteadFirst other <> _ = InsteadFirst other _ <> InsteadFirst other = InsteadFirst other |
Defined in Acme.Functors Methods (<>) :: OrInsteadFirst otherThing a -> OrInsteadFirst otherThing a -> OrInsteadFirst otherThing a # sconcat :: NonEmpty (OrInsteadFirst otherThing a) -> OrInsteadFirst otherThing a # stimes :: Integral b => b -> OrInsteadFirst otherThing a -> OrInsteadFirst otherThing a # | |
(Semigroup otherThing, Monoid a) => Monoid (OrInsteadFirst otherThing a) Source # | mempty = NotInsteadFirst mempty |
Defined in Acme.Functors Methods mempty :: OrInsteadFirst otherThing a Source # |
Determined-by-parameter
data DeterminedBy parameter a Source #
DeterminedBy parameter
is a value that... well, we're not really sure
what it is. We'll find out once a parameter
is provided.
The mechanism for deciding how the value is determined from the
parameter
is opaque; all you can do is test it with different parameters
and see what results. There aren't even Eq
or Show
instances, which is
annoying.
Constructors
Determination ((->) parameter a) |
Instances
Monad (DeterminedBy parameter) Source # | Determination fa >>= ff = Determination (\x -> let Determination f = ff (fa x) in f x) |
Defined in Acme.Functors Methods (>>=) :: DeterminedBy parameter a -> (a -> DeterminedBy parameter b) -> DeterminedBy parameter b # (>>) :: DeterminedBy parameter a -> DeterminedBy parameter b -> DeterminedBy parameter b # return :: a -> DeterminedBy parameter a # | |
Functor (DeterminedBy parameter) Source # | |
Defined in Acme.Functors Methods fmap :: (a -> b) -> DeterminedBy parameter a -> DeterminedBy parameter b # (<$) :: a -> DeterminedBy parameter b -> DeterminedBy parameter a # | |
Applicative (DeterminedBy parameter) Source # | pure a = Determination (\_ -> a) Determination f <*> Determination a = Determination (\x -> f x (a x)) |
Defined in Acme.Functors Methods pure :: a -> DeterminedBy parameter a # (<*>) :: DeterminedBy parameter (a -> b) -> DeterminedBy parameter a -> DeterminedBy parameter b # liftA2 :: (a -> b -> c) -> DeterminedBy parameter a -> DeterminedBy parameter b -> DeterminedBy parameter c # (*>) :: DeterminedBy parameter a -> DeterminedBy parameter b -> DeterminedBy parameter b # (<*) :: DeterminedBy parameter a -> DeterminedBy parameter b -> DeterminedBy parameter a # | |
Semigroup a => Semigroup (DeterminedBy parameter a) Source # | Determination f <> Determination g = Determination (\x -> f x <> g x) |
Defined in Acme.Functors Methods (<>) :: DeterminedBy parameter a -> DeterminedBy parameter a -> DeterminedBy parameter a # sconcat :: NonEmpty (DeterminedBy parameter a) -> DeterminedBy parameter a # stimes :: Integral b => b -> DeterminedBy parameter a -> DeterminedBy parameter a # | |
Monoid a => Monoid (DeterminedBy parameter a) Source # | mempty = Determination (\_ -> mempty) |
Defined in Acme.Functors Methods mempty :: DeterminedBy parameter a Source # |