Skip to content

re-add Alternate newtype #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Data/Monoid/Alternate.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Data.Monoid.Alternate where

import Prelude

import Control.Alternative (class Alt, class Plus, class Alternative, empty, (<|>))
import Control.Comonad (class Comonad, class Extend)
import Data.Eq (class Eq1)
import Data.Ord (class Ord1)

-- | Monoid and semigroup instances corresponding to `Plus` and `Alt` instances
-- | for `f`
-- |
-- | ``` purescript
-- | Alternate fx <> Alternate fy == Alternate (fx <|> fy)
-- | mempty :: Alternate _ == Alternate empty
-- | ```
newtype Alternate f a = Alternate (f a)

derive newtype instance eqAlternate :: Eq (f a) => Eq (Alternate f a)

derive newtype instance eq1Alternate :: Eq1 f => Eq1 (Alternate f)

derive newtype instance ordAlternate :: Ord (f a) => Ord (Alternate f a)

derive newtype instance ord1Alternate :: Ord1 f => Ord1 (Alternate f)

derive newtype instance boundedAlternate :: Bounded (f a) => Bounded (Alternate f a)

derive newtype instance functorAlternate :: Functor f => Functor (Alternate f)

derive newtype instance applyAlternate :: Apply f => Apply (Alternate f)

derive newtype instance applicativeAlternate :: Applicative f => Applicative (Alternate f)

derive newtype instance altAlternate :: Alt f => Alt (Alternate f)

derive newtype instance plusAlternate :: Plus f => Plus (Alternate f)

derive newtype instance alternativeAlternate :: Alternative f => Alternative (Alternate f)

derive newtype instance bindAlternate :: Bind f => Bind (Alternate f)

derive newtype instance monadAlternate :: Monad f => Monad (Alternate f)

derive newtype instance extendAlternate :: Extend f => Extend (Alternate f)

derive newtype instance comonadAlternate :: Comonad f => Comonad (Alternate f)

instance showAlternate :: Show (f a) => Show (Alternate f a) where
show (Alternate a) = "(Alternate " <> show a <> ")"

instance semigroupAlternate :: Alt f => Semigroup (Alternate f a) where
append (Alternate a) (Alternate b) = Alternate (a <|> b)

instance monoidAlternate :: Plus f => Monoid (Alternate f a) where
mempty = Alternate empty