Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Type Class Instances

instance altMaybe :: Alt Maybe

instance alternativeMaybe :: Alternative Maybe

instance applicativeMaybe :: Applicative Maybe
Expand All @@ -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)


Expand Down
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"bower.json",
"Gruntfile.js",
"package.json"
]
],
"dependencies": {
"purescript-control": "~0.2.0"
}
}
115 changes: 63 additions & 52 deletions src/Data/Maybe.purs
Original file line number Diff line number Diff line change
@@ -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