Skip to content

Commit 85aa2db

Browse files
authored
Merge pull request #217 from JordanMartinez/addDocs
Expand on documentation
2 parents 268870d + 6e14a53 commit 85aa2db

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

src/Control/Apply.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ import Control.Category (identity)
2626
-- | the function application operator `($)` to arguments wrapped with the
2727
-- | type constructor `f`.
2828
-- |
29+
-- | Put differently...
30+
-- | ```
31+
-- | foo =
32+
-- | functionTakingNArguments <$> computationProducingArg1
33+
-- | <*> computationProducingArg2
34+
-- | <*> ...
35+
-- | <*> computationProducingArgN
36+
-- | ```
37+
-- |
2938
-- | Instances must satisfy the following law in addition to the `Functor`
3039
-- | laws:
3140
-- |

src/Control/Bind.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ infixr 1 bindFlipped as =<<
6363
instance bindFn :: Bind ((->) r) where
6464
bind m f x = f (m x) x
6565

66+
-- | The `bind`/`>>=` function for `Array` works by applying a function to
67+
-- | each element in the array, and flattening the results into a single,
68+
-- | new array.
69+
-- |
70+
-- | Array's `bind`/`>>=` works like a nested for loop. Each `bind` adds
71+
-- | another level of nesting in the loop. For example:
72+
-- | ```
73+
-- | foo :: Array String
74+
-- | foo =
75+
-- | ["a", "b"] >>= \eachElementInArray1 ->
76+
-- | ["c", "d"] >>= \eachElementInArray2
77+
-- | pure (eachElementInArray1 <> eachElementInArray2)
78+
-- |
79+
-- | -- In other words...
80+
-- | foo
81+
-- | -- ... is the same as...
82+
-- | [ ("a" <> "c"), ("a" <> "d"), ("b" <> "c"), ("b" <> "d") ]
83+
-- | -- which simplifies to...
84+
-- | [ "ac", "ad", "bc", "bd" ]
85+
-- | ```
6686
instance bindArray :: Bind Array where
6787
bind = arrayBind
6888

src/Control/Monad.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Data.Unit (Unit)
3131
class (Applicative m, Bind m) <= Monad m
3232

3333
instance monadFn :: Monad ((->) r)
34+
3435
instance monadArray :: Monad Array
3536

3637
-- | `liftM1` provides a default implementation of `(<$>)` for any

src/Data/Monoid.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ import Type.Data.RowList (RLProxy(..))
2828
-- | `Monoid`s are commonly used as the result of fold operations, where
2929
-- | `<>` is used to combine individual results, and `mempty` gives the result
3030
-- | of folding an empty collection of elements.
31+
-- |
32+
-- | ### Newtypes for Monoid
33+
-- |
34+
-- | Some types (e.g. `Int`, `Boolean`) can implement multiple law-abiding
35+
-- | instances for `Monoid`. Let's use `Int` as an example
36+
-- | 1. `<>` could be `+` and `mempty` could be `0`
37+
-- | 2. `<>` could be `*` and `mempty` could be `1`.
38+
-- |
39+
-- | To clarify these ambiguous situations, one should use the newtypes
40+
-- | defined in `Data.Monoid.<NewtypeName>` modules.
41+
-- |
42+
-- | In the above ambiguous situation, we could use `Additive`
43+
-- | for the first situation or `Multiplicative` for the second one.
3144
class Semigroup m <= Monoid m where
3245
mempty :: m
3346

src/Data/Semigroup.purs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ import Type.Data.RowList (RLProxy(..))
1818
-- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
1919
-- |
2020
-- | One example of a `Semigroup` is `String`, with `(<>)` defined as string
21-
-- | concatenation.
21+
-- | concatenation. Another example is `List a`, with `(<>)` defined as
22+
-- | list concatenation.
23+
-- |
24+
-- | ### Newtypes for Semigroup
25+
-- |
26+
-- | There are two other ways to implement an instance for this type class
27+
-- | regardless of which type is used. These instances can be used by
28+
-- | wrapping the values in one of the two newtypes below:
29+
-- | 1. `First` - Use the first argument every time: `append first _ = first`.
30+
-- | 2. `Last` - Use the last argument every time: `append _ last = last`.
2231
class Semigroup a where
2332
append :: a -> a -> a
2433

src/Data/Void.purs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@ module Data.Void (Void, absurd) where
22

33
import Data.Show (class Show)
44

5-
-- | An uninhabited data type.
5+
-- | An uninhabited data type. In other words, one can never create
6+
-- | a runtime value of type `Void` becaue no such value exists.
67
-- |
78
-- | `Void` is useful to eliminate the possibility of a value being created.
89
-- | For example, a value of type `Either Void Boolean` can never have
910
-- | a Left value created in PureScript.
11+
-- |
12+
-- | This should not be confused with the keyword `void` that commonly appears in
13+
-- | C-family languages, such as Java:
14+
-- | ```
15+
-- | public class Foo {
16+
-- | void doSomething() { System.out.println("hello world!"); }
17+
-- | }
18+
-- | ```
19+
-- |
20+
-- | In PureScript, one often uses `Unit` to achieve similar effects as
21+
-- | the `void` of C-family languages above.
1022
newtype Void = Void Void
1123

1224
instance showVoid :: Show Void where

0 commit comments

Comments
 (0)