File tree 4 files changed +14
-56
lines changed 4 files changed +14
-56
lines changed Original file line number Diff line number Diff line change 2
2
3
3
module Control.Alt where
4
4
5
- infixl 3 <|>
6
-
7
5
-- | The `Alt` type class identifies an associative operation on a type
8
6
-- | constructor. It is similar to `Semigroup`, except that it applies to
9
7
-- | types of kind `* -> *`, like `Array` or `List`, rather than concrete types
@@ -17,4 +15,10 @@ infixl 3 <|>
17
15
-- | For example, the `Array` (`[]`) type is an instance of `Alt`, where
18
16
-- | `(<|>)` is defined to be concatenation.
19
17
class (Functor f ) <= Alt f where
20
- (<|>) :: forall a . f a -> f a -> f a
18
+ alt :: forall a . f a -> f a -> f a
19
+
20
+ infixl 3 <|>
21
+
22
+ -- | An infix version of `alt`.
23
+ (<|>) :: forall f a . (Alt f ) => f a -> f a -> f a
24
+ (<|>) = alt
Original file line number Diff line number Diff line change @@ -17,18 +17,3 @@ import Control.Plus
17
17
-- | - Annihilation: `empty <*> f = empty`
18
18
class (Applicative f , Plus f ) <= Alternative f
19
19
20
- -- | Attempt a computation multiple times, requiring at least one success.
21
- -- |
22
- -- | The `Lazy` constraint is used to generate the result lazily, to ensure
23
- -- | termination.
24
- some :: forall f a . (Alternative f , Lazy (f [a ])) => f a -> f [a ]
25
- some v = (:) <$> v <*> defer (\_ -> many v)
26
-
27
- -- | Attempt a computation multiple times, returning as many successful results
28
- -- | as possible (possibly zero).
29
- -- |
30
- -- | The `Lazy` constraint is used to generate the result lazily, to ensure
31
- -- | termination.
32
- many :: forall f a . (Alternative f , Lazy (f [a ])) => f a -> f [a ]
33
- many v = some v <|> pure []
34
-
Original file line number Diff line number Diff line change @@ -18,10 +18,14 @@ infixr 1 =<=
18
18
-- |
19
19
-- | - Associativity: `extend f <<< extend g = extend (f <<< extend g)`
20
20
class (Functor w ) <= Extend w where
21
- (<<=) :: forall b a . (w a -> b ) -> w a -> w b
21
+ extend :: forall b a . (w a -> b ) -> w a -> w b
22
22
23
23
instance extendArr :: (Semigroup w ) => Extend ((-> ) w ) where
24
- (<<=) f g w = f \w' -> g (w <> w')
24
+ extend f g w = f \w' -> g (w <> w')
25
+
26
+ -- | An infix version of `extend`
27
+ (<<=) :: forall w a b . (Extend w ) => (w a -> b ) -> w a -> w b
28
+ (<<=) = extend
25
29
26
30
-- | A version of `(<<=)` with its arguments flipped.
27
31
(=>>) :: forall b a w . (Extend w ) => w a -> (w a -> b ) -> w b
@@ -35,12 +39,8 @@ instance extendArr :: (Semigroup w) => Extend ((->) w) where
35
39
(=<=) :: forall b a w c . (Extend w ) => (w b -> c ) -> (w a -> b ) -> w a -> c
36
40
(=<=) f g w = f (g <<= w)
37
41
38
- -- | An alias for `(<<=)`.
39
- extend :: forall b a w . (Extend w ) => (w a -> b ) -> w a -> w b
40
- extend = (<<=)
41
-
42
42
-- | Duplicate a comonadic context.
43
43
-- |
44
44
-- | `duplicate` is dual to `Control.Bind.join`.
45
45
duplicate :: forall a w . (Extend w ) => w a -> w (w a )
46
- duplicate w = id <<= w
46
+ duplicate = extend id
Original file line number Diff line number Diff line change 2
2
3
3
module Control.Monad where
4
4
5
- import Data.Int (Int ())
6
-
7
- -- | Perform a monadic action `n` times collecting all of the results.
8
- replicateM :: forall m a . (Monad m ) => Int -> m a -> m [a ]
9
- replicateM n m | n < one = return []
10
- | otherwise = do a <- m
11
- as <- replicateM (n - one) m
12
- return (a : as)
13
-
14
- -- | Perform a fold using a monadic step function.
15
- foldM :: forall m a b . (Monad m ) => (a -> b -> m a ) -> a -> [b ] -> m a
16
- foldM _ a [] = return a
17
- foldM f a (b:bs) = f a b >>= \a' -> foldM f a' bs
18
-
19
5
-- | Perform a monadic action when a condition is true.
20
6
when :: forall m . (Monad m ) => Boolean -> m Unit -> m Unit
21
7
when true m = m
@@ -25,20 +11,3 @@ when false _ = return unit
25
11
unless :: forall m . (Monad m ) => Boolean -> m Unit -> m Unit
26
12
unless false m = m
27
13
unless true _ = return unit
28
-
29
- -- | Filter where the predicate returns a monadic `Boolean`.
30
- -- |
31
- -- | For example:
32
- -- |
33
- -- | ```purescript
34
- -- | powerSet :: forall a. [a] -> [[a]]
35
- -- | powerSet = filterM (const [true, false])
36
- -- | ```
37
- filterM :: forall a m . (Monad m ) => (a -> m Boolean ) -> [a ] -> m [a ]
38
- filterM _ [] = return []
39
- filterM p (x:xs) = do
40
- b <- p x
41
- xs' <- filterM p xs
42
- return $ if b
43
- then x : xs'
44
- else xs'
You can’t perform that action at this time.
0 commit comments