From 0a8095d9168df4518d6914e729bd3632729baaea Mon Sep 17 00:00:00 2001 From: Nicholas Scheel Date: Mon, 5 Mar 2018 10:30:01 -0600 Subject: [PATCH] Add MonadZero instances Such that Alt corresponds with Semigroup and Plus with Monoid --- src/Data/Maybe/First.purs | 14 ++++++++++++-- src/Data/Maybe/Last.purs | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Data/Maybe/First.purs b/src/Data/Maybe/First.purs index 74a4105..2b46478 100644 --- a/src/Data/Maybe/First.purs +++ b/src/Data/Maybe/First.purs @@ -3,11 +3,11 @@ module Data.Maybe.First where import Prelude import Control.Extend (class Extend) - +import Control.MonadZero (class MonadZero, class Alternative, class Plus, class Alt) import Data.Eq (class Eq1) import Data.Functor.Invariant (class Invariant) import Data.Maybe (Maybe(..)) -import Data.Monoid (class Monoid) +import Data.Monoid (class Monoid, mempty) import Data.Newtype (class Newtype) import Data.Ord (class Ord1) @@ -56,3 +56,13 @@ instance semigroupFirst :: Semigroup (First a) where instance monoidFirst :: Monoid (First a) where mempty = First Nothing + +instance altFirst :: Alt First where + alt = append + +instance plusFirst :: Plus First where + empty = mempty + +instance alternativeFirst :: Alternative First + +instance monadZeroFirst :: MonadZero First diff --git a/src/Data/Maybe/Last.purs b/src/Data/Maybe/Last.purs index b719fac..71f8c11 100644 --- a/src/Data/Maybe/Last.purs +++ b/src/Data/Maybe/Last.purs @@ -3,11 +3,11 @@ module Data.Maybe.Last where import Prelude import Control.Extend (class Extend) - +import Control.MonadZero (class MonadZero, class Alternative, class Plus, class Alt) import Data.Eq (class Eq1) import Data.Functor.Invariant (class Invariant) import Data.Maybe (Maybe(..)) -import Data.Monoid (class Monoid) +import Data.Monoid (class Monoid, mempty) import Data.Newtype (class Newtype) import Data.Ord (class Ord1) @@ -56,3 +56,13 @@ instance semigroupLast :: Semigroup (Last a) where instance monoidLast :: Monoid (Last a) where mempty = Last Nothing + +instance altLast :: Alt Last where + alt = append + +instance plusLast :: Plus Last where + empty = mempty + +instance alternativeLast :: Alternative Last + +instance monadZeroLast :: MonadZero Last