@@ -30,22 +30,22 @@ import Prelude
30
30
31
31
-- | Perform some operation on 'Just', given the field inside the 'Just'.
32
32
--
33
- -- > whenJust Nothing print == return ()
33
+ -- > whenJust Nothing print == pure ()
34
34
-- > whenJust (Just 1) print == print 1
35
35
whenJust :: Applicative m => Maybe a -> (a -> m () ) -> m ()
36
36
whenJust mg f = maybe (pure () ) f mg
37
37
38
38
-- | Like 'whenJust', but where the test can be monadic.
39
39
whenJustM :: Monad m => m (Maybe a ) -> (a -> m () ) -> m ()
40
40
-- Can't reuse whenMaybe on GHC 7.8 or lower because Monad does not imply Applicative
41
- whenJustM mg f = maybeM (return () ) f mg
41
+ whenJustM mg f = maybeM (pure () ) f mg
42
42
43
43
44
44
-- | Like 'when', but return either 'Nothing' if the predicate was 'False',
45
45
-- of 'Just' with the result of the computation.
46
46
--
47
47
-- > whenMaybe True (print 1) == fmap Just (print 1)
48
- -- > whenMaybe False (print 1) == return Nothing
48
+ -- > whenMaybe False (print 1) == pure Nothing
49
49
whenMaybe :: Applicative m => Bool -> m a -> m (Maybe a )
50
50
whenMaybe b x = if b then Just <$> x else pure Nothing
51
51
@@ -54,7 +54,7 @@ whenMaybeM :: Monad m => m Bool -> m a -> m (Maybe a)
54
54
-- Can't reuse whenMaybe on GHC 7.8 or lower because Monad does not imply Applicative
55
55
whenMaybeM mb x = do
56
56
b <- mb
57
- if b then liftM Just x else return Nothing
57
+ if b then liftM Just x else pure Nothing
58
58
59
59
60
60
-- | The identity function which requires the inner argument to be @()@. Useful for functions
@@ -89,7 +89,7 @@ fold1M f xs = error "fold1M: empty list"
89
89
90
90
-- | Like 'fold1M' but discards the result.
91
91
fold1M_ :: (Partial , Monad m ) => (a -> a -> m a ) -> [a ] -> m ()
92
- fold1M_ f xs = fold1M f xs >> return ()
92
+ fold1M_ f xs = fold1M f xs >> pure ()
93
93
94
94
95
95
-- Data.List for Monad
@@ -99,18 +99,18 @@ fold1M_ f xs = fold1M f xs >> return ()
99
99
-- > partitionM (Just . even) [1,2,3] == Just ([2], [1,3])
100
100
-- > partitionM (const Nothing) [1,2,3] == Nothing
101
101
partitionM :: Monad m => (a -> m Bool ) -> [a ] -> m ([a ], [a ])
102
- partitionM f [] = return ([] , [] )
102
+ partitionM f [] = pure ([] , [] )
103
103
partitionM f (x: xs) = do
104
104
res <- f x
105
105
(as,bs) <- partitionM f xs
106
- return ([x | res]++ as, [x | not res]++ bs)
106
+ pure ([x | res]++ as, [x | not res]++ bs)
107
107
108
108
109
109
-- | A version of 'concatMap' that works with a monadic predicate.
110
110
concatMapM :: Monad m => (a -> m [b ]) -> [a ] -> m [b ]
111
111
{-# INLINE concatMapM #-}
112
- concatMapM op = foldr f (return [] )
113
- where f x xs = do x <- op x; if null x then xs else do xs <- xs; return $ x++ xs
112
+ concatMapM op = foldr f (pure [] )
113
+ where f x xs = do x <- op x; if null x then xs else do xs <- xs; pure $ x++ xs
114
114
115
115
-- | Like 'concatMapM', but has its arguments flipped, so can be used
116
116
-- instead of the common @fmap concat $ forM@ pattern.
@@ -124,8 +124,8 @@ mconcatMapM f = liftM mconcat . mapM f
124
124
-- | A version of 'mapMaybe' that works with a monadic predicate.
125
125
mapMaybeM :: Monad m => (a -> m (Maybe b )) -> [a ] -> m [b ]
126
126
{-# INLINE mapMaybeM #-}
127
- mapMaybeM op = foldr f (return [] )
128
- where f x xs = do x <- op x; case x of Nothing -> xs; Just x -> do xs <- xs; return $ x: xs
127
+ mapMaybeM op = foldr f (pure [] )
128
+ where f x xs = do x <- op x; case x of Nothing -> xs; Just x -> do xs <- xs; pure $ x: xs
129
129
130
130
-- Looping
131
131
@@ -145,7 +145,7 @@ loopM act x = do
145
145
res <- act x
146
146
case res of
147
147
Left x -> loopM act x
148
- Right v -> return v
148
+ Right v -> pure v
149
149
150
150
-- | Keep running an operation until it becomes 'False'. As an example:
151
151
--
@@ -164,11 +164,11 @@ whileM act = do
164
164
165
165
-- | Like 'when', but where the test can be monadic.
166
166
whenM :: Monad m => m Bool -> m () -> m ()
167
- whenM b t = ifM b t (return () )
167
+ whenM b t = ifM b t (pure () )
168
168
169
169
-- | Like 'unless', but where the test can be monadic.
170
170
unlessM :: Monad m => m Bool -> m () -> m ()
171
- unlessM b f = ifM b (return () ) f
171
+ unlessM b f = ifM b (pure () ) f
172
172
173
173
-- | Like @if@, but where the test can be monadic.
174
174
ifM :: Monad m => m Bool -> m a -> m a -> m a
@@ -186,7 +186,7 @@ notM = fmap not
186
186
-- > Just False ||^ Just True == Just True
187
187
-- > Just False ||^ Just False == Just False
188
188
(||^) :: Monad m => m Bool -> m Bool -> m Bool
189
- (||^) a b = ifM a (return True ) b
189
+ (||^) a b = ifM a (pure True ) b
190
190
191
191
-- | The lazy '&&' operator lifted to a monad. If the first
192
192
-- argument evaluates to 'False' the second argument will not
@@ -196,23 +196,23 @@ notM = fmap not
196
196
-- > Just True &&^ Just True == Just True
197
197
-- > Just True &&^ Just False == Just False
198
198
(&&^) :: Monad m => m Bool -> m Bool -> m Bool
199
- (&&^) a b = ifM a b (return False )
199
+ (&&^) a b = ifM a b (pure False )
200
200
201
201
-- | A version of 'any' lifted to a monad. Retains the short-circuiting behaviour.
202
202
--
203
203
-- > anyM Just [False,True ,undefined] == Just True
204
204
-- > anyM Just [False,False,undefined] == undefined
205
205
-- > \(f :: Int -> Maybe Bool) xs -> anyM f xs == orM (map f xs)
206
206
anyM :: Monad m => (a -> m Bool ) -> [a ] -> m Bool
207
- anyM p = foldr ((||^) . p) (return False )
207
+ anyM p = foldr ((||^) . p) (pure False )
208
208
209
209
-- | A version of 'all' lifted to a monad. Retains the short-circuiting behaviour.
210
210
--
211
211
-- > allM Just [True,False,undefined] == Just False
212
212
-- > allM Just [True,True ,undefined] == undefined
213
213
-- > \(f :: Int -> Maybe Bool) xs -> anyM f xs == orM (map f xs)
214
214
allM :: Monad m => (a -> m Bool ) -> [a ] -> m Bool
215
- allM p = foldr ((&&^) . p) (return True )
215
+ allM p = foldr ((&&^) . p) (pure True )
216
216
217
217
-- | A version of 'or' lifted to a monad. Retains the short-circuiting behaviour.
218
218
--
@@ -238,9 +238,9 @@ andM = allM id
238
238
-- > findM (Just . isUpper) "test" == Just Nothing
239
239
-- > findM (Just . const True) ["x",undefined] == Just (Just "x")
240
240
findM :: Monad m => (a -> m Bool ) -> [a ] -> m (Maybe a )
241
- findM p = foldr (\ x -> ifM (p x) (return $ Just x)) (return Nothing )
241
+ findM p = foldr (\ x -> ifM (p x) (pure $ Just x)) (pure Nothing )
242
242
243
243
-- | Like 'findM', but also allows you to compute some additional information in the predicate.
244
244
firstJustM :: Monad m => (a -> m (Maybe b )) -> [a ] -> m (Maybe b )
245
- firstJustM p [] = return Nothing
246
- firstJustM p (x: xs) = maybeM (firstJustM p xs) (return . Just ) (p x)
245
+ firstJustM p [] = pure Nothing
246
+ firstJustM p (x: xs) = maybeM (firstJustM p xs) (pure . Just ) (p x)
0 commit comments