diff --git a/README.md b/README.md index c5122bd..35f4d5d 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ ### Type Class Instances + instance altMaybe :: Alt Maybe + instance alternativeMaybe :: Alternative Maybe instance applicativeMaybe :: Applicative Maybe @@ -25,8 +27,12 @@ instance monadMaybe :: Monad Maybe + instance monadPlusMaybe :: MonadPlus Maybe + instance ordMaybe :: (Ord a) => Ord (Maybe a) + instance plusMaybe :: Plus Maybe + instance showMaybe :: (Show a) => Show (Maybe a) diff --git a/bower.json b/bower.json index f02a221..e0efb60 100644 --- a/bower.json +++ b/bower.json @@ -16,5 +16,8 @@ "bower.json", "Gruntfile.js", "package.json" - ] + ], + "dependencies": { + "purescript-control": "~0.2.0" + } } diff --git a/src/Data/Maybe.purs b/src/Data/Maybe.purs index 3ee96b7..74de17e 100644 --- a/src/Data/Maybe.purs +++ b/src/Data/Maybe.purs @@ -1,54 +1,65 @@ module Data.Maybe where - data Maybe a = Nothing | Just a - - maybe :: forall a b. b -> (a -> b) -> Maybe a -> b - maybe b _ Nothing = b - maybe _ f (Just a) = f a - - fromMaybe :: forall a. a -> Maybe a -> a - fromMaybe a = maybe a (id :: forall a. a -> a) - - isJust :: forall a. Maybe a -> Boolean - isJust = maybe false (const true) - - isNothing :: forall a. Maybe a -> Boolean - isNothing = maybe true (const false) - - instance functorMaybe :: Functor Maybe where - (<$>) fn (Just x) = Just (fn x) - (<$>) _ _ = Nothing - - instance applyMaybe :: Apply Maybe where - (<*>) (Just fn) x = fn <$> x - (<*>) Nothing _ = Nothing - - instance applicativeMaybe :: Applicative Maybe where - pure = Just - - instance alternativeMaybe :: Alternative Maybe where - empty = Nothing - (<|>) Nothing r = r - (<|>) l _ = l - - instance bindMaybe :: Bind Maybe where - (>>=) (Just x) k = k x - (>>=) Nothing _ = Nothing - - instance monadMaybe :: Monad Maybe - - instance showMaybe :: (Show a) => Show (Maybe a) where - show (Just x) = "Just (" ++ show x ++ ")" - show Nothing = "Nothing" - - instance eqMaybe :: (Eq a) => Eq (Maybe a) where - (==) Nothing Nothing = true - (==) (Just a1) (Just a2) = a1 == a2 - (==) _ _ = false - (/=) a b = not (a == b) - - 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 +import Control.Alt +import Control.Plus +import Control.Alternative +import Control.MonadPlus + +data Maybe a = Nothing | Just a + +maybe :: forall a b. b -> (a -> b) -> Maybe a -> b +maybe b _ Nothing = b +maybe _ f (Just a) = f a + +fromMaybe :: forall a. a -> Maybe a -> a +fromMaybe a = maybe a (id :: forall a. a -> a) + +isJust :: forall a. Maybe a -> Boolean +isJust = maybe false (const true) + +isNothing :: forall a. Maybe a -> Boolean +isNothing = maybe true (const false) + +instance functorMaybe :: Functor Maybe where + (<$>) fn (Just x) = Just (fn x) + (<$>) _ _ = Nothing + +instance applyMaybe :: Apply Maybe where + (<*>) (Just fn) x = fn <$> x + (<*>) Nothing _ = Nothing + +instance applicativeMaybe :: Applicative Maybe where + pure = Just + +instance altMaybe :: Alt Maybe where + (<|>) Nothing r = r + (<|>) l _ = l + +instance plusMaybe :: Plus Maybe where + empty = Nothing + +instance alternativeMaybe :: Alternative Maybe + +instance bindMaybe :: Bind Maybe where + (>>=) (Just x) k = k x + (>>=) Nothing _ = Nothing + +instance monadMaybe :: Monad Maybe + +instance monadPlusMaybe :: MonadPlus Maybe + +instance showMaybe :: (Show a) => Show (Maybe a) where + show (Just x) = "Just (" ++ show x ++ ")" + show Nothing = "Nothing" + +instance eqMaybe :: (Eq a) => Eq (Maybe a) where + (==) Nothing Nothing = true + (==) (Just a1) (Just a2) = a1 == a2 + (==) _ _ = false + (/=) a b = not (a == b) + +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