Skip to content

Commit b844a35

Browse files
committed
More docs improvements
- Give every function at least one example - Use List rather than Array (for consistency) - Fix a couple of minor errors (e.g. extra argument to `range`)
1 parent f492853 commit b844a35

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/Data/Unfoldable.purs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ foreign import unfoldrArrayImpl
5555
-- | For example:
5656
-- |
5757
-- | ``` purescript
58-
-- | replicate 2 "foo" == ["foo", "foo"] :: Array String
58+
-- | replicate 2 "foo" == (["foo", "foo"] :: Array String)
5959
-- | ```
6060
replicate :: forall f a. Unfoldable f => Int -> a -> f a
6161
replicate n v = unfoldr step n
@@ -66,6 +66,11 @@ replicate n v = unfoldr step n
6666
else Just (Tuple v (i - 1))
6767

6868
-- | Perform an Applicative action `n` times, and accumulate all the results.
69+
-- |
70+
-- | ``` purescript
71+
-- | > replicateA 5 (randomInt 1 10) :: Effect (Array Int)
72+
-- | [1,3,2,7,5]
73+
-- | ```
6974
replicateA
7075
:: forall m f a
7176
. Applicative m
@@ -80,11 +85,16 @@ replicateA n m = sequence (replicate n m)
8085
-- | For example:
8186
-- |
8287
-- | ``` purescript
83-
-- | none == [] :: forall a. Array a
88+
-- | none == ([] :: Array Unit)
8489
-- | ```
8590
none :: forall f a. Unfoldable f => f a
8691
none = unfoldr (const Nothing) unit
8792

88-
-- | Convert a Maybe to any Unfoldable like lists and arrays.
93+
-- | Convert a Maybe to any Unfoldable, such as lists or arrays.
94+
-- |
95+
-- | ``` purescript
96+
-- | fromMaybe (Nothing :: Maybe Int) == []
97+
-- | fromMaybe (Just 1) == [1]
98+
-- | ```
8999
fromMaybe :: forall f a. Unfoldable f => Maybe a -> f a
90100
fromMaybe = unfoldr (\b -> flip Tuple Nothing <$> b)

src/Data/Unfoldable1.purs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import Partial.Unsafe (unsafePartial)
2121
-- |
2222
-- | Note that, in order to provide an `Unfoldable1 t` instance, `t` need not
2323
-- | be a type which is guaranteed to be non-empty. For example, the fact that
24-
-- | arrays can be empty does not prevent us from providing an `Unfoldable1
25-
-- | Array` instance. However, the result of `unfoldr1` should always be
26-
-- | non-empty.
24+
-- | lists can be empty does not prevent us from providing an
25+
-- | `Unfoldable1 List` instance. However, the result of `unfoldr1` should
26+
-- | always be non-empty.
2727
-- |
2828
-- | Every type which has an `Unfoldable` instance can be given an
2929
-- | `Unfoldable1` instance (and, in fact, is required to, because
3030
-- | `Unfoldable1` is a superclass of `Unfoldable`). However, there are types
3131
-- | which have `Unfoldable1` instances but cannot have `Unfoldable` instances.
3232
-- | In particular, types which are guaranteed to be non-empty, such as
33-
-- | `NonEmptyArray`, cannot be given `Unfoldable` instances.
33+
-- | `NonEmptyList`, cannot be given `Unfoldable` instances.
3434
-- |
3535
-- | The utility of this class, then, is that it provides an `Unfoldable`-like
3636
-- | interface while still permitting instances for guaranteed-non-empty types
37-
-- | like `NonEmptyArray`.
37+
-- | like `NonEmptyList`.
3838
class Unfoldable1 t where
3939
unfoldr1 :: forall a b. (b -> Tuple a (Maybe b)) -> b -> t a
4040

@@ -52,11 +52,11 @@ foreign import unfoldr1ArrayImpl
5252
-> Array a
5353

5454
-- | Replicate a value `n` times. At least one value will be produced, so values
55-
-- | `n < 1` less than one will be ignored.
55+
-- | `n` less than 1 will be treated as 1.
5656
-- |
5757
-- | ``` purescript
58-
-- | replicate1 0 "foo" == NEL.singleton "foo" :: NEL.NonEmptyList String
59-
-- | replicate1 2 "foo" == NEL.cons "foo" (NEL.singleton "foo") :: NEL.NonEmptyList String
58+
-- | replicate1 2 "foo" == (NEL.cons "foo" (NEL.singleton "foo") :: NEL.NonEmptyList String)
59+
-- | replicate1 0 "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
6060
-- | ```
6161
replicate1 :: forall f a. Unfoldable1 f => Int -> a -> f a
6262
replicate1 n v = unfoldr1 step (n - 1)
@@ -66,8 +66,15 @@ replicate1 n v = unfoldr1 step (n - 1)
6666
| i <= 0 = Tuple v Nothing
6767
| otherwise = Tuple v (Just (i - 1))
6868

69-
-- | Perform an `Apply` action `n` times (at least once, so values `n < 1`
70-
-- | less than one will be ignored), and accumulate the results.
69+
-- | Perform an `Apply` action `n` times (at least once, so values `n` less
70+
-- | than 1 will be treated as 1), and accumulate the results.
71+
-- |
72+
-- | ``` purescript
73+
-- | > replicate1A 2 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
74+
-- | (NonEmptyList (NonEmpty 8 (2 : Nil)))
75+
-- | > replicate1A 0 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
76+
-- | (NonEmptyList (NonEmpty 4 Nil))
77+
-- | ```
7178
replicate1A
7279
:: forall m f a
7380
. Apply m
@@ -81,7 +88,7 @@ replicate1A n m = sequence1 (replicate1 n m)
8188
-- | Contain a single value. For example:
8289
-- |
8390
-- | ``` purescript
84-
-- | singleton "foo" == NEL.singleton "foo" :: NEL.NonEmptyList String
91+
-- | singleton "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
8592
-- | ```
8693
singleton :: forall f a. Unfoldable1 f => a -> f a
8794
singleton = replicate1 1
@@ -90,9 +97,9 @@ singleton = replicate1 1
9097
-- | endpoints.
9198
-- |
9299
-- | ``` purescript
93-
-- | range 0 0 "foo" == NEL.singleton 0 :: NEL.NonEmptyList Int
94-
-- | range 1 2 "foo" == NEL.cons 1 (NEL.singleton 2) :: NEL.NonEmptyList Int
95-
-- | range 2 0 "foo" == NEL.cons 2 (NEL.cons 1 (NEL.singleton 0)) :: NEL.NonEmptyList Int
100+
-- | range 0 0 == (NEL.singleton 0 :: NEL.NonEmptyList Int)
101+
-- | range 1 2 == (NEL.cons 1 (NEL.singleton 2) :: NEL.NonEmptyList Int)
102+
-- | range 2 0 == (NEL.cons 2 (NEL.cons 1 (NEL.singleton 0)) :: NEL.NonEmptyList Int)
96103
-- | ```
97104
range :: forall f. Unfoldable1 f => Int -> Int -> f Int
98105
range start end =

0 commit comments

Comments
 (0)