Skip to content

Commit 66b69c9

Browse files
authored
Merge pull request #24 from purescript/expand-docs
Expand docs
2 parents 1135c42 + 1dcc2d1 commit 66b69c9

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Data/Unfoldable.purs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ import Data.Tuple (Tuple(..), fst, snd)
2121
import Data.Unfoldable1 (class Unfoldable1, unfoldr1, singleton, range, replicate1, replicate1A)
2222
import Partial.Unsafe (unsafePartial)
2323

24-
-- | This class identifies data structures which can be _unfolded_.
24+
-- | This class identifies (possibly empty) data structures which can be
25+
-- | _unfolded_.
2526
-- |
26-
-- | The generating function `f` in `unfoldr f` in understood as follows:
27+
-- | The generating function `f` in `unfoldr f` is understood as follows:
2728
-- |
2829
-- | - If `f b` is `Nothing`, then `unfoldr f b` should be empty.
2930
-- | - If `f b` is `Just (Tuple a b1)`, then `unfoldr f b` should consist of `a`
3031
-- | appended to the result of `unfoldr f b1`.
32+
-- |
33+
-- | Note that it is not possible to give `Unfoldable` instances to types which
34+
-- | represent structures which are guaranteed to be non-empty, such as
35+
-- | `NonEmptyArray`: consider what `unfoldr (const Nothing)` should produce.
36+
-- | Structures which are guaranteed to be non-empty can instead be given
37+
-- | `Unfoldable1` instances.
3138
class Unfoldable1 t <= Unfoldable t where
3239
unfoldr :: forall a b. (b -> Maybe (Tuple a b)) -> b -> t a
3340

src/Data/Unfoldable1.purs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,28 @@ import Data.Semigroup.Traversable (class Traversable1, sequence1)
1313
import Data.Tuple (Tuple(..), fst, snd)
1414
import Partial.Unsafe (unsafePartial)
1515

16-
-- | This class identifies non-empty data structures which can be _unfolded_.
16+
-- | This class identifies data structures which can be _unfolded_.
1717
-- |
18-
-- | The generating function `f` corresponds to the `uncons` operation of a
19-
-- | non-empty list or array; it always return a value, and then optionally
20-
-- | a value to continue unfolding from.
18+
-- | The generating function `f` in `unfoldr1 f` corresponds to the `uncons`
19+
-- | operation of a non-empty list or array; it always returns a value, and
20+
-- | then optionally a value to continue unfolding from.
21+
-- |
22+
-- | Note that, in order to provide an `Unfoldable1 t` instance, `t` need not
23+
-- | 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.
27+
-- |
28+
-- | Every type which has an `Unfoldable` instance can be given an
29+
-- | `Unfoldable1` instance (and, in fact, is required to, because
30+
-- | `Unfoldable1` is a superclass of `Unfoldable`). However, there are types
31+
-- | which have `Unfoldable1` instances but cannot have `Unfoldable` instances.
32+
-- | In particular, types which are guaranteed to be non-empty, such as
33+
-- | `NonEmptyArray`, cannot be given `Unfoldable` instances.
34+
-- |
35+
-- | The utility of this class, then, is that it provides an `Unfoldable`-like
36+
-- | interface while still permitting instances for guaranteed-non-empty types
37+
-- | like `NonEmptyArray`.
2138
class Unfoldable1 t where
2239
unfoldr1 :: forall a b. (b -> Tuple a (Maybe b)) -> b -> t a
2340

0 commit comments

Comments
 (0)