Skip to content

Commit 9033b04

Browse files
committed
Merge pull request #4 from purescript/alternative
Add Alt, Plus, MonadPlus, update Alternative
2 parents 7294fb8 + dbcb71f commit 9033b04

File tree

3 files changed

+73
-53
lines changed

3 files changed

+73
-53
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
### Type Class Instances
1313

14+
instance altMaybe :: Alt Maybe
15+
1416
instance alternativeMaybe :: Alternative Maybe
1517

1618
instance applicativeMaybe :: Applicative Maybe
@@ -25,8 +27,12 @@
2527

2628
instance monadMaybe :: Monad Maybe
2729

30+
instance monadPlusMaybe :: MonadPlus Maybe
31+
2832
instance ordMaybe :: (Ord a) => Ord (Maybe a)
2933

34+
instance plusMaybe :: Plus Maybe
35+
3036
instance showMaybe :: (Show a) => Show (Maybe a)
3137

3238

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
"bower.json",
1717
"Gruntfile.js",
1818
"package.json"
19-
]
19+
],
20+
"dependencies": {
21+
"purescript-control": "~0.2.0"
22+
}
2023
}

src/Data/Maybe.purs

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,65 @@
11
module Data.Maybe where
22

3-
data Maybe a = Nothing | Just a
4-
5-
maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
6-
maybe b _ Nothing = b
7-
maybe _ f (Just a) = f a
8-
9-
fromMaybe :: forall a. a -> Maybe a -> a
10-
fromMaybe a = maybe a (id :: forall a. a -> a)
11-
12-
isJust :: forall a. Maybe a -> Boolean
13-
isJust = maybe false (const true)
14-
15-
isNothing :: forall a. Maybe a -> Boolean
16-
isNothing = maybe true (const false)
17-
18-
instance functorMaybe :: Functor Maybe where
19-
(<$>) fn (Just x) = Just (fn x)
20-
(<$>) _ _ = Nothing
21-
22-
instance applyMaybe :: Apply Maybe where
23-
(<*>) (Just fn) x = fn <$> x
24-
(<*>) Nothing _ = Nothing
25-
26-
instance applicativeMaybe :: Applicative Maybe where
27-
pure = Just
28-
29-
instance alternativeMaybe :: Alternative Maybe where
30-
empty = Nothing
31-
(<|>) Nothing r = r
32-
(<|>) l _ = l
33-
34-
instance bindMaybe :: Bind Maybe where
35-
(>>=) (Just x) k = k x
36-
(>>=) Nothing _ = Nothing
37-
38-
instance monadMaybe :: Monad Maybe
39-
40-
instance showMaybe :: (Show a) => Show (Maybe a) where
41-
show (Just x) = "Just (" ++ show x ++ ")"
42-
show Nothing = "Nothing"
43-
44-
instance eqMaybe :: (Eq a) => Eq (Maybe a) where
45-
(==) Nothing Nothing = true
46-
(==) (Just a1) (Just a2) = a1 == a2
47-
(==) _ _ = false
48-
(/=) a b = not (a == b)
49-
50-
instance ordMaybe :: (Ord a) => Ord (Maybe a) where
51-
compare (Just x) (Just y) = compare x y
52-
compare Nothing Nothing = EQ
53-
compare Nothing _ = LT
54-
compare _ Nothing = GT
3+
import Control.Alt
4+
import Control.Plus
5+
import Control.Alternative
6+
import Control.MonadPlus
7+
8+
data Maybe a = Nothing | Just a
9+
10+
maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
11+
maybe b _ Nothing = b
12+
maybe _ f (Just a) = f a
13+
14+
fromMaybe :: forall a. a -> Maybe a -> a
15+
fromMaybe a = maybe a (id :: forall a. a -> a)
16+
17+
isJust :: forall a. Maybe a -> Boolean
18+
isJust = maybe false (const true)
19+
20+
isNothing :: forall a. Maybe a -> Boolean
21+
isNothing = maybe true (const false)
22+
23+
instance functorMaybe :: Functor Maybe where
24+
(<$>) fn (Just x) = Just (fn x)
25+
(<$>) _ _ = Nothing
26+
27+
instance applyMaybe :: Apply Maybe where
28+
(<*>) (Just fn) x = fn <$> x
29+
(<*>) Nothing _ = Nothing
30+
31+
instance applicativeMaybe :: Applicative Maybe where
32+
pure = Just
33+
34+
instance altMaybe :: Alt Maybe where
35+
(<|>) Nothing r = r
36+
(<|>) l _ = l
37+
38+
instance plusMaybe :: Plus Maybe where
39+
empty = Nothing
40+
41+
instance alternativeMaybe :: Alternative Maybe
42+
43+
instance bindMaybe :: Bind Maybe where
44+
(>>=) (Just x) k = k x
45+
(>>=) Nothing _ = Nothing
46+
47+
instance monadMaybe :: Monad Maybe
48+
49+
instance monadPlusMaybe :: MonadPlus Maybe
50+
51+
instance showMaybe :: (Show a) => Show (Maybe a) where
52+
show (Just x) = "Just (" ++ show x ++ ")"
53+
show Nothing = "Nothing"
54+
55+
instance eqMaybe :: (Eq a) => Eq (Maybe a) where
56+
(==) Nothing Nothing = true
57+
(==) (Just a1) (Just a2) = a1 == a2
58+
(==) _ _ = false
59+
(/=) a b = not (a == b)
60+
61+
instance ordMaybe :: (Ord a) => Ord (Maybe a) where
62+
compare (Just x) (Just y) = compare x y
63+
compare Nothing Nothing = EQ
64+
compare Nothing _ = LT
65+
compare _ Nothing = GT

0 commit comments

Comments
 (0)