From a29b2487051d52342abe745c0cd5565d77cb0185 Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Sun, 2 Oct 2016 22:33:37 +0100 Subject: [PATCH 1/2] Prepare for 2.0 release --- bower.json | 2 +- src/Data/Maybe.purs | 26 ++++---------------------- src/Data/Maybe/First.purs | 29 ++++++++--------------------- src/Data/Maybe/Last.purs | 29 ++++++++--------------------- 4 files changed, 21 insertions(+), 65 deletions(-) diff --git a/bower.json b/bower.json index b1c0d81..2839faa 100644 --- a/bower.json +++ b/bower.json @@ -17,6 +17,6 @@ "package.json" ], "dependencies": { - "purescript-monoid": "^1.0.0" + "purescript-monoid": "^2.0.0" } } diff --git a/src/Data/Maybe.purs b/src/Data/Maybe.purs index 95fbf0f..0db8d47 100644 --- a/src/Data/Maybe.purs +++ b/src/Data/Maybe.purs @@ -1,26 +1,15 @@ module Data.Maybe where +import Prelude + import Control.Alt (class Alt) import Control.Alternative (class Alternative) -import Control.Applicative (class Applicative) -import Control.Apply (class Apply) -import Control.Bind (class Bind) import Control.Extend (class Extend) -import Control.Monad (class Monad) import Control.MonadZero (class MonadZero) import Control.Plus (class Plus) -import Data.Bounded (class Bounded, top) -import Data.Eq (class Eq, (==)) -import Data.Function (const, id) -import Data.Functor (class Functor, (<$>)) import Data.Functor.Invariant (class Invariant, imapF) import Data.Monoid (class Monoid) -import Data.Ord (class Ord, compare) -import Data.Ordering (Ordering(..)) -import Data.Semigroup (class Semigroup, (<>)) -import Data.Show (class Show, show) -import Data.Unit (Unit, unit) -- | The `Maybe` type is used to represent optional values and can be seen as -- | something like a type-safe `null`, where `Nothing` is `null` and `Just x` @@ -193,21 +182,14 @@ instance monoidMaybe :: Semigroup a => Monoid (Maybe a) where -- | The `Eq` instance allows `Maybe` values to be checked for equality with -- | `==` and inequality with `/=` whenever there is an `Eq` instance for the -- | type the `Maybe` contains. -instance eqMaybe :: Eq a => Eq (Maybe a) where - eq Nothing Nothing = true - eq (Just a1) (Just a2) = a1 == a2 - eq _ _ = false +derive instance eqMaybe :: Eq a => Eq (Maybe a) -- | The `Ord` instance allows `Maybe` values to be compared with -- | `compare`, `>`, `>=`, `<` and `<=` whenever there is an `Ord` instance for -- | the type the `Maybe` contains. -- | -- | `Nothing` is considered to be less than any `Just` value. -instance ordMaybe :: Ord a => Ord (Maybe a) where - compare (Just x) (Just y) = compare x y - compare Nothing Nothing = EQ - compare Nothing _ = LT - compare _ Nothing = GT +derive instance ordMaybe :: Ord a => Ord (Maybe a) instance boundedMaybe :: Bounded a => Bounded (Maybe a) where top = Just top diff --git a/src/Data/Maybe/First.purs b/src/Data/Maybe/First.purs index 12458c6..2dcf2c2 100644 --- a/src/Data/Maybe/First.purs +++ b/src/Data/Maybe/First.purs @@ -1,21 +1,13 @@ module Data.Maybe.First where -import Control.Applicative (class Applicative, pure) -import Control.Apply (class Apply, (<*>)) -import Control.Bind (class Bind, bind) +import Prelude + import Control.Extend (class Extend, extend) -import Control.Monad (class Monad) -import Data.Bounded (class Bounded, top, bottom) -import Data.Eq (class Eq, (==)) -import Data.Function ((<<<)) -import Data.Functor (class Functor, (<$>)) import Data.Functor.Invariant (class Invariant, imapF) import Data.Maybe (Maybe(..)) import Data.Monoid (class Monoid) -import Data.Ord (class Ord, compare) -import Data.Semigroup (class Semigroup, (<>)) -import Data.Show (class Show, show) +import Data.Newtype (class Newtype) -- | Monoid returning the first (left-most) non-`Nothing` value. -- | @@ -27,18 +19,13 @@ import Data.Show (class Show, show) -- | ``` newtype First a = First (Maybe a) -runFirst :: forall a. First a -> Maybe a -runFirst (First m) = m +derive instance newtypeFirst :: Newtype (First a) _ -instance eqFirst :: (Eq a) => Eq (First a) where - eq (First x) (First y) = x == y +derive newtype instance eqFirst :: (Eq a) => Eq (First a) -instance ordFirst :: (Ord a) => Ord (First a) where - compare (First x) (First y) = compare x y +derive newtype instance ordFirst :: (Ord a) => Ord (First a) -instance boundedFirst :: (Bounded a) => Bounded (First a) where - top = First top - bottom = First bottom +derive newtype instance boundedFirst :: (Bounded a) => Bounded (First a) instance functorFirst :: Functor First where map f (First x) = First (f <$> x) @@ -53,7 +40,7 @@ instance applicativeFirst :: Applicative First where pure = First <<< pure instance bindFirst :: Bind First where - bind (First x) f = First (bind x (runFirst <<< f)) + bind (First x) f = First (x >>= \y -> case f y of First ma -> ma) instance monadFirst :: Monad First diff --git a/src/Data/Maybe/Last.purs b/src/Data/Maybe/Last.purs index 762575b..85a44b2 100644 --- a/src/Data/Maybe/Last.purs +++ b/src/Data/Maybe/Last.purs @@ -1,21 +1,13 @@ module Data.Maybe.Last where -import Control.Applicative (class Applicative, pure) -import Control.Apply (class Apply, (<*>)) -import Control.Bind (class Bind, bind) +import Prelude + import Control.Extend (class Extend, extend) -import Control.Monad (class Monad) -import Data.Bounded (class Bounded, top, bottom) -import Data.Eq (class Eq, (==)) -import Data.Function ((<<<)) -import Data.Functor (class Functor, (<$>)) import Data.Functor.Invariant (class Invariant, imapF) import Data.Maybe (Maybe(..)) import Data.Monoid (class Monoid) -import Data.Ord (class Ord, compare) -import Data.Semigroup (class Semigroup, (<>)) -import Data.Show (class Show, show) +import Data.Newtype (class Newtype) -- | Monoid returning the last (right-most) non-`Nothing` value. -- | @@ -27,18 +19,13 @@ import Data.Show (class Show, show) -- | ``` newtype Last a = Last (Maybe a) -runLast :: forall a. Last a -> Maybe a -runLast (Last m) = m +derive instance newtypeLast :: Newtype (Last a) _ -instance eqLast :: Eq a => Eq (Last a) where - eq (Last x) (Last y) = x == y +derive newtype instance eqLast :: Eq a => Eq (Last a) -instance ordLast :: Ord a => Ord (Last a) where - compare (Last x) (Last y) = compare x y +derive newtype instance ordLast :: Ord a => Ord (Last a) -instance boundedLast :: Bounded a => Bounded (Last a) where - top = Last top - bottom = Last bottom +derive newtype instance boundedLast :: Bounded a => Bounded (Last a) instance functorLast :: Functor Last where map f (Last x) = Last (f <$> x) @@ -53,7 +40,7 @@ instance applicativeLast :: Applicative Last where pure = Last <<< pure instance bindLast :: Bind Last where - bind (Last x) f = Last (bind x (runLast <<< f)) + bind (Last x) f = Last (x >>= \y -> case f y of Last ma -> ma) instance monadLast :: Monad Last From e3aaa6cac8150d23f1f338d8205f216c7e7b84ce Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Mon, 3 Oct 2016 00:00:57 +0100 Subject: [PATCH 2/2] `newtype derive` more stuff! --- src/Data/Maybe/First.purs | 24 +++++++++--------------- src/Data/Maybe/Last.purs | 30 ++++++++++++------------------ 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/Data/Maybe/First.purs b/src/Data/Maybe/First.purs index 2dcf2c2..f346c6d 100644 --- a/src/Data/Maybe/First.purs +++ b/src/Data/Maybe/First.purs @@ -2,9 +2,9 @@ module Data.Maybe.First where import Prelude -import Control.Extend (class Extend, extend) +import Control.Extend (class Extend) -import Data.Functor.Invariant (class Invariant, imapF) +import Data.Functor.Invariant (class Invariant) import Data.Maybe (Maybe(..)) import Data.Monoid (class Monoid) import Data.Newtype (class Newtype) @@ -27,25 +27,19 @@ derive newtype instance ordFirst :: (Ord a) => Ord (First a) derive newtype instance boundedFirst :: (Bounded a) => Bounded (First a) -instance functorFirst :: Functor First where - map f (First x) = First (f <$> x) +derive newtype instance functorFirst :: Functor First -instance invariantFirst :: Invariant First where - imap = imapF +derive newtype instance invariantFirst :: Invariant First -instance applyFirst :: Apply First where - apply (First f) (First x) = First (f <*> x) +derive newtype instance applyFirst :: Apply First -instance applicativeFirst :: Applicative First where - pure = First <<< pure +derive newtype instance applicativeFirst :: Applicative First -instance bindFirst :: Bind First where - bind (First x) f = First (x >>= \y -> case f y of First ma -> ma) +derive newtype instance bindFirst :: Bind First -instance monadFirst :: Monad First +derive newtype instance monadFirst :: Monad First -instance extendFirst :: Extend First where - extend f (First x) = First (extend (f <<< First) x) +derive newtype instance extendFirst :: Extend First instance showFirst :: (Show a) => Show (First a) where show (First a) = "First (" <> show a <> ")" diff --git a/src/Data/Maybe/Last.purs b/src/Data/Maybe/Last.purs index 85a44b2..9383a46 100644 --- a/src/Data/Maybe/Last.purs +++ b/src/Data/Maybe/Last.purs @@ -2,9 +2,9 @@ module Data.Maybe.Last where import Prelude -import Control.Extend (class Extend, extend) +import Control.Extend (class Extend) -import Data.Functor.Invariant (class Invariant, imapF) +import Data.Functor.Invariant (class Invariant) import Data.Maybe (Maybe(..)) import Data.Monoid (class Monoid) import Data.Newtype (class Newtype) @@ -21,31 +21,25 @@ newtype Last a = Last (Maybe a) derive instance newtypeLast :: Newtype (Last a) _ -derive newtype instance eqLast :: Eq a => Eq (Last a) +derive newtype instance eqLast :: (Eq a) => Eq (Last a) -derive newtype instance ordLast :: Ord a => Ord (Last a) +derive newtype instance ordLast :: (Ord a) => Ord (Last a) -derive newtype instance boundedLast :: Bounded a => Bounded (Last a) +derive newtype instance boundedLast :: (Bounded a) => Bounded (Last a) -instance functorLast :: Functor Last where - map f (Last x) = Last (f <$> x) +derive newtype instance functorLast :: Functor Last -instance invariantLast :: Invariant Last where - imap = imapF +derive newtype instance invariantLast :: Invariant Last -instance applyLast :: Apply Last where - apply (Last f) (Last x) = Last (f <*> x) +derive newtype instance applyLast :: Apply Last -instance applicativeLast :: Applicative Last where - pure = Last <<< pure +derive newtype instance applicativeLast :: Applicative Last -instance bindLast :: Bind Last where - bind (Last x) f = Last (x >>= \y -> case f y of Last ma -> ma) +derive newtype instance bindLast :: Bind Last -instance monadLast :: Monad Last +derive newtype instance monadLast :: Monad Last -instance extendLast :: Extend Last where - extend f (Last x) = Last (extend (f <<< Last) x) +derive newtype instance extendLast :: Extend Last instance showLast :: Show a => Show (Last a) where show (Last a) = "(Last " <> show a <> ")"