Skip to content

Add more documentation examples #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/Data/Unfoldable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,27 @@ import Partial.Unsafe (unsafePartial)
-- | This class identifies (possibly empty) data structures which can be
-- | _unfolded_.
-- |
-- | Just as `Foldable` structures can be collapsed to a single value with
-- | `foldl` and a provided _folding_ function, `unfoldr` allows generating
-- | an `Unfoldable` structure from a provided _unfolding_ function and an
-- | initial seed value.
-- |
-- | The generating function `f` in `unfoldr f` is understood as follows:
-- |
-- | - If `f b` is `Nothing`, then `unfoldr f b` should be empty.
-- | - If `f b` is `Just (Tuple a b1)`, then `unfoldr f b` should consist of `a`
-- | appended to the result of `unfoldr f b1`.
-- |
-- | For example:
-- | ``` purescript
-- | f :: Int -> Maybe (Tuple Int Int)
-- | f n | n < 0 = Nothing
-- | f n = Just (Tuple n (n - 1))
-- |
-- | unfoldr f 3 == [3, 2, 1, 0]
-- | unfoldr f 3 == 3 : 2 : 1 : 0 : Nil
-- | ```
-- |
-- | Note that it is not possible to give `Unfoldable` instances to types which
-- | represent structures which are guaranteed to be non-empty, such as
-- | `NonEmptyArray`: consider what `unfoldr (const Nothing)` should produce.
Expand All @@ -55,10 +70,10 @@ foreign import unfoldrArrayImpl
-> Array a

-- | Replicate a value some natural number of times.
-- | For example:
-- |
-- | ``` purescript
-- | replicate 2 "foo" == (["foo", "foo"] :: Array String)
-- | replicate 2 "foo" == ["foo", "foo"]
-- | replicate 2 "foo" == "foo" : "foo" : Nil
-- | ```
replicate :: forall f a. Unfoldable f => Int -> a -> f a
replicate n v = unfoldr step n
Expand All @@ -73,6 +88,8 @@ replicate n v = unfoldr step n
-- | ``` purescript
-- | > replicateA 5 (randomInt 1 10) :: Effect (Array Int)
-- | [1,3,2,7,5]
-- | > replicateA 5 (randomInt 1 10) :: _ (List _)
-- | (4 : 4 : 9 : 8 : 1 : Nil)
-- | ```
replicateA
:: forall m f a
Expand All @@ -85,10 +102,11 @@ replicateA
replicateA n m = sequence (replicate n m)

-- | The container with no elements - unfolded with zero iterations.
-- | For example:
-- |
-- | ``` purescript
-- | none == ([] :: Array Unit)
-- | none == ([] :: _ Int)
-- | none == (Nil :: _ Number)
-- | ```
none :: forall f a. Unfoldable f => f a
none = unfoldr (const Nothing) unit
Expand All @@ -98,6 +116,7 @@ none = unfoldr (const Nothing) unit
-- | ``` purescript
-- | fromMaybe (Nothing :: Maybe Int) == []
-- | fromMaybe (Just 1) == [1]
-- | fromMaybe (Just 1) == 1 : Nil
-- | ```
fromMaybe :: forall f a. Unfoldable f => Maybe a -> f a
fromMaybe = unfoldr (\b -> flip Tuple Nothing <$> b)
34 changes: 28 additions & 6 deletions src/Data/Unfoldable1.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ import Partial.Unsafe (unsafePartial)
-- | operation of a non-empty list or array; it always returns a value, and
-- | then optionally a value to continue unfolding from.
-- |
-- | For example:
-- | ``` purescript
-- | g :: Int -> Tuple Int (Maybe Int)
-- | g n | n <= 0 = Tuple 0 Nothing
-- | g n = Tuple n (Just (n - 1))
-- |
-- | unfoldr1 g 3 == NEA.cons' 3 [2, 1, 0]
-- | unfoldr1 g 3 == NEL.cons' 3 (2 : 1 : 0 : Nil)
-- | -- Also produces structures of types which can be empty
-- | unfoldr1 g 3 == [3, 2, 1, 0]
-- | unfoldr1 g 3 == 3 : 2 : 1 : 0 : Nil
Comment on lines +28 to +32
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this REPL-style sequence is clearer:

> unfoldr1 g 3 :: NonEmptyArray Int
(NonEmptyArray [3,2,1,0])
> unfoldr1 g 3 :: NonEmptyList _
(NonEmptyList (NonEmpty 3 (2 : 1 : 0 : Nil)))
> unfoldr1 g 3 :: Array _
[3,2,1,0]
> unfoldr1 g 3 :: List _
(3 : 2 : 1 : 0 : Nil)

Also, there's some inconsistency with show for NonEmptyArray vs NonEmptyList. The Array version is a lot more concise.

-- | ```
-- |
-- | Note that, in order to provide an `Unfoldable1 t` instance, `t` need not
-- | be a type which is guaranteed to be non-empty. For example, the fact that
-- | lists can be empty does not prevent us from providing an
Expand Down Expand Up @@ -58,8 +71,14 @@ foreign import unfoldr1ArrayImpl
-- | `n` less than 1 will be treated as 1.
-- |
-- | ``` purescript
-- | replicate1 2 "foo" == (NEL.cons "foo" (NEL.singleton "foo") :: NEL.NonEmptyList String)
-- | replicate1 0 "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
-- | > replicate1 2 "foo" :: NonEmptyList _
-- | (NonEmptyList (NonEmpty "foo" ("foo" : Nil)))
-- | > replicate1 0 "foo" :: NonEmptyArray _
-- | (NonEmptyArray ["foo"])
-- | > replicate1 0 "foo" :: List _
-- | ("foo" : Nil)
-- | > replicate1 2 "foo" :: Array _
-- | ["foo","foo"]
-- | ```
replicate1 :: forall f a. Unfoldable1 f => Int -> a -> f a
replicate1 n v = unfoldr1 step (n - 1)
Expand All @@ -73,10 +92,10 @@ replicate1 n v = unfoldr1 step (n - 1)
-- | than 1 will be treated as 1), and accumulate the results.
-- |
-- | ``` purescript
-- | > replicate1A 2 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
-- | > replicate1A 2 (randomInt 1 10) :: Effect (NonEmptyList Int)
-- | (NonEmptyList (NonEmpty 8 (2 : Nil)))
-- | > replicate1A 0 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
-- | (NonEmptyList (NonEmpty 4 Nil))
-- | > replicate1A 0 (randomInt 1 10) :: _ (NonEmptyArray _)
-- | (NonEmptyArray [3])
Comment on lines +95 to +98
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these work too? Missing Traversable1:

replicate1A 0 (randomInt 1 10) :: _ (List _)
replicate1A 2 (randomInt 1 10) :: _ (Array _)

-- | ```
replicate1A
:: forall m f a
Expand All @@ -91,7 +110,8 @@ replicate1A n m = sequence1 (replicate1 n m)
-- | Contain a single value. For example:
-- |
-- | ``` purescript
-- | singleton "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
-- | singleton "foo" == (NEL.singleton "foo" :: NonEmptyList String)
-- | singleton "foo" == (NEA.singleton "foo" :: NonEmptyArray String)
-- | ```
singleton :: forall f a. Unfoldable1 f => a -> f a
singleton = replicate1 1
Expand All @@ -103,6 +123,8 @@ singleton = replicate1 1
-- | range 0 0 == (NEL.singleton 0 :: NEL.NonEmptyList Int)
-- | range 1 2 == (NEL.cons 1 (NEL.singleton 2) :: NEL.NonEmptyList Int)
-- | range 2 0 == (NEL.cons 2 (NEL.cons 1 (NEL.singleton 0)) :: NEL.NonEmptyList Int)
Comment on lines 123 to 125
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing the Non-empty examples is kinda clumsy without purescript/purescript-lists#190

-- | range 1 3 == [1, 2, 3]
-- | range 1 3 == 1 : 2 : 3 : Nil
-- | ```
range :: forall f. Unfoldable1 f => Int -> Int -> f Int
range start end =
Expand Down