Skip to content

Commit a559c91

Browse files
committed
Merge pull request #19 from purescript/docs
Docs
2 parents 65da234 + f7e6cb5 commit a559c91

File tree

12 files changed

+264
-11
lines changed

12 files changed

+264
-11
lines changed

README.md

Lines changed: 137 additions & 7 deletions
Large diffs are not rendered by default.

src/Control/Alt.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- | This module defines the `Alt` type class.
2+
13
module Control.Alt where
24

35
infixl 3 <|>

src/Control/Alternative.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- | This module defines the `Alternative` type class and associated
2+
-- | helper functions.
3+
14
module Control.Alternative where
25

36
import Control.Alt
@@ -14,9 +17,18 @@ import Control.Plus
1417
-- | - Annihilation: `empty <*> f = empty`
1518
class (Applicative f, Plus f) <= Alternative f
1619

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.
1724
some :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
1825
some v = (:) <$> v <*> defer1 (\_ -> many v)
1926

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.
2032
many :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
2133
many v = some v <|> pure []
2234

src/Control/Apply.purs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1+
-- | This module defines helper functions for working with `Apply` instances.
2+
13
module Control.Apply where
24

35
infixl 4 <*
46
infixl 4 *>
57

8+
-- | Combine two effectful actions, keeping only the result of the first.
69
(<*) :: forall a b f. (Apply f) => f a -> f b -> f a
710
(<*) a b = const <$> a <*> b
811

12+
-- | Combine two effectful actions, keeping only the result of the second.
913
(*>) :: forall a b f. (Apply f) => f a -> f b -> f b
1014
(*>) a b = const id <$> a <*> b
1115

16+
-- | Lift a function of two arguments to a function which accepts and returns
17+
-- | values wrapped with the type constructor `f`.
1218
lift2 :: forall a b c f. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
1319
lift2 f a b = f <$> a <*> b
1420

21+
-- | Lift a function of three arguments to a function which accepts and returns
22+
-- | values wrapped with the type constructor `f`.
1523
lift3 :: forall a b c d f. (Apply f) => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
1624
lift3 f a b c = f <$> a <*> b <*> c
1725

26+
-- | Lift a function of four arguments to a function which accepts and returns
27+
-- | values wrapped with the type constructor `f`.
1828
lift4 :: forall a b c d e f. (Apply f) => (a -> b -> c -> d -> e) -> f a -> f b -> f c -> f d -> f e
1929
lift4 f a b c d = f <$> a <*> b <*> c <*> d
2030

31+
-- | Lift a function of five arguments to a function which accepts and returns
32+
-- | values wrapped with the type constructor `f`.
2133
lift5 :: forall a b c d e f g. (Apply f) => (a -> b -> c -> d -> e -> g) -> f a -> f b -> f c -> f d -> f e -> f g
2234
lift5 f a b c d e = f <$> a <*> b <*> c <*> d <*> e
23-
24-
forever :: forall a b f. (Apply f) => f a -> f b
25-
forever a = a *> forever a

src/Control/Bind.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1+
-- | This module defines helper functions for working with `Bind` instances.
2+
13
module Control.Bind where
24

35
infixr 1 =<<
46
infixr 1 >=>
57
infixr 1 <=<
68

9+
-- | A version of `(>>=)` with its arguments flipped.
710
(=<<) :: forall a b m. (Bind m) => (a -> m b) -> m a -> m b
811
(=<<) f m = m >>= f
912

13+
-- | Forwards Kleisli composition.
14+
-- |
15+
-- | For example:
16+
-- |
17+
-- | ```purescript
18+
-- | import Data.Array (head, tail)
19+
-- |
20+
-- | third = tail >=> tail >=> head
21+
-- | ```
1022
(>=>) :: forall a b c m. (Bind m) => (a -> m b) -> (b -> m c) -> a -> m c
1123
(>=>) f g a = f a >>= g
1224

25+
-- | Backwards Kleisli composition.
1326
(<=<) :: forall a b c m. (Bind m) => (b -> m c) -> (a -> m b) -> a -> m c
1427
(<=<) f g a = f =<< g a
1528

29+
-- | Collapse two applications of a monadic type constructor into one.
1630
join :: forall a m. (Bind m) => m (m a) -> m a
1731
join m = m >>= id
1832

33+
-- | Execute a monadic action if a condition holds.
34+
-- |
35+
-- | For example:
36+
-- |
37+
-- | ```purescript
38+
-- | main = ifM ((< 0.5) <$> random)
39+
-- | (trace "Heads")
40+
-- | (trace "Tails")
41+
-- | ```
1942
ifM :: forall a m. (Bind m) => m Boolean -> m a -> m a -> m a
2043
ifM cond t f = cond >>= \cond' -> if cond' then t else f

src/Control/Comonad.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
-- | This module defines the `Comonad` type class.
2+
13
module Control.Comonad where
24

35
import Control.Extend
46

7+
-- | `Comonad` extends the `Extend` class with the `extract` function
8+
-- | which extracts a value, discarding the comonadic context.
9+
-- |
10+
-- | `Comonad` is the dual of `Monad`, and `extract` is the dual of
11+
-- | `pure` or `return`.
12+
-- |
13+
-- | Laws:
14+
-- |
15+
-- | - Left Identity: `extract <<= xs = xs`
16+
-- | - Right Identity: `extract (f <<= xs) = f xs`
517
class (Extend w) <= Comonad w where
618
extract :: forall a. w a -> a

src/Control/Extend.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
1+
-- | This module defines the `Extend` type class and associated helper functions.
2+
13
module Control.Extend where
24

35
infixl 1 =>>
46
infixr 1 <<=
57
infixr 1 =>=
68
infixr 1 =<=
79

10+
-- | The `Extend` class defines the extension operator `(<<=)`
11+
-- | which extends a local context-dependent computation to
12+
-- | a global computation.
13+
-- |
14+
-- | `Extend` is the dual of `Bind`, and `(<<=)` is the dual of
15+
-- | `(>>=)`.
16+
-- |
17+
-- | Laws:
18+
-- |
19+
-- | - Associativity: `extend f <<< extend g = extend (f <<< extend g)`
820
class (Functor w) <= Extend w where
921
(<<=) :: forall b a. (w a -> b) -> w a -> w b
1022

1123
instance extendArr :: (Semigroup w) => Extend ((->) w) where
1224
(<<=) f g w = f \w' -> g (w <> w')
1325

26+
-- | A version of `(<<=)` with its arguments flipped.
1427
(=>>) :: forall b a w. (Extend w) => w a -> (w a -> b) -> w b
1528
(=>>) w f = f <<= w
1629

30+
-- | Forwards co-Kleisli composition.
1731
(=>=) :: forall b a w c. (Extend w) => (w a -> b) -> (w b -> c) -> w a -> c
1832
(=>=) f g w = g (f <<= w)
1933

34+
-- | Backwards co-Kleisli composition.
2035
(=<=) :: forall b a w c. (Extend w) => (w b -> c) -> (w a -> b) -> w a -> c
2136
(=<=) f g w = f (g <<= w)
2237

38+
-- | Duplicate a comonadic context.
39+
-- |
40+
-- | `duplicate` is dual to `Control.Bind.join`.
2341
duplicate :: forall a w. (Extend w) => w a -> w (w a)
2442
duplicate w = id <<= w

src/Control/Functor.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
-- | This module defines helper functions for working with `Functor` instances.
2+
13
module Control.Functor where
24

35
infixl 4 <$
46
infixl 4 $>
57

8+
-- | Ignore the return value of a computation, using the specified return value instead.
69
(<$) :: forall f a b. (Functor f) => a -> f b -> f a
710
(<$) x f = const x <$> f
811

12+
-- | A version of `(<$)` with its arguments flipped.
913
($>) :: forall f a b. (Functor f) => f a -> b -> f b
1014
($>) f x = const x <$> f

src/Control/Lazy.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
-- | This module defines the `Lazy` type class and associated
2+
-- | helper functions.
3+
14
module Control.Lazy where
25

6+
-- | The `Lazy` class represents types which allow evaluation of values
7+
-- | to be _deferred_.
8+
-- |
9+
-- | Usually, this means that a type contains a function arrow which can
10+
-- | be used to delay evaluation.
311
class Lazy l where
412
defer :: (Unit -> l) -> l
513

14+
-- | A version of `Lazy` for type constructors of one type argument.
615
class Lazy1 l where
716
defer1 :: forall a. (Unit -> l a) -> l a
817

18+
-- | A version of `Lazy` for type constructors of two type arguments.
919
class Lazy2 l where
1020
defer2 :: forall a b. (Unit -> l a b) -> l a b
1121

22+
-- | `fix` defines a value as the fixed point of a function.
23+
-- |
24+
-- | The `Lazy` instance allows us to generate the result lazily.
1225
fix :: forall l a. (Lazy l) => (l -> l) -> l
1326
fix f = defer (\_ -> f (fix f))
1427

28+
-- | A version of `fix` for type constructors of one type argument.
1529
fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a
1630
fix1 f = defer1 (\_ -> f (fix1 f))
1731

32+
-- | A version of `fix` for type constructors of two type arguments.
1833
fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b
1934
fix2 f = defer2 (\_ -> f (fix2 f))

src/Control/Monad.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
-- | This module defines helper functions for working with `Monad` instances.
2+
13
module Control.Monad where
24

5+
-- | Perform a monadic action `n` times collecting all of the results.
36
replicateM :: forall m a. (Monad m) => Number -> m a -> m [a]
47
replicateM 0 _ = return []
58
replicateM n m = do
69
a <- m
710
as <- replicateM (n - 1) m
811
return (a : as)
912

13+
-- | Perform a fold using a monadic step function.
1014
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
1115
foldM _ a [] = return a
1216
foldM f a (b:bs) = f a b >>= \a' -> foldM f a' bs
1317

18+
-- | Perform a monadic action when a condition is true.
1419
when :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
1520
when true m = m
1621
when false _ = return unit
1722

23+
-- | Perform a monadic action unless a condition is true.
1824
unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
1925
unless false m = m
2026
unless true _ = return unit
2127

22-
-- | Filter where the predicate returns a monadic Boolean. For example:
28+
-- | Filter where the predicate returns a monadic `Boolean`.
29+
-- |
30+
-- | For example:
2331
-- |
2432
-- | ```purescript
2533
-- | powerSet :: forall a. [a] -> [[a]]

0 commit comments

Comments
 (0)