Skip to content

Add examples #36

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

Merged
merged 3 commits into from
Dec 6, 2020
Merged
Changes from 1 commit
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
68 changes: 67 additions & 1 deletion src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,99 @@ import Data.Unfoldable1 (class Unfoldable1)
-- | For example:
-- |
-- | ```purescript
-- | import Data.NonEmpty
-- |
-- | nonEmptyArray :: NonEmpty Array Int
-- | nonEmptyArray = NonEmpty 1 [2,3]
-- |
-- | import Data.List(List(..), (:))
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be preferable to move the import declarations to the top, as you aren't allowed to have any import declarations after the first non-import declaration in a module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying to match the style of using the repl, and keeping things logically clustered. I guess it's a question of whether this slight addition of clarity/organization outweighs the confusion if someone pastes this into a .purs file. I think the error message is pretty clear though in that case.

Luckily this won't be a concern with doctest (repl style).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this a blocker? I'm fine with changing it, but it doesn't seem too consequential either way, so I'll spare the effort if it's unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, it’s just that changes to the core libraries should generally obtain two core team approvals, see https://github.com/purescript/governance

-- |
-- | nonEmptyList :: NonEmpty List Int
-- | nonEmptyList = 0 :| empty
-- | nonEmptyList = NonEmpty 1 (2 : 3 : Nil)
-- | ```
data NonEmpty f a = NonEmpty a (f a)

-- | An infix synonym for `NonEmpty`.
-- |
-- | For example:
-- |
-- | ```purescript
-- | nonEmptyArray :: NonEmpty Array Int
-- | nonEmptyArray = 1 :| [2,3]
-- |
-- | nonEmptyList :: NonEmpty List Int
-- | nonEmptyList = 1 :| 2 : 3 : Nil
-- | ```
infixr 5 NonEmpty as :|

-- | Create a non-empty structure with a single value.
-- |
-- | For example:
-- |
-- | ```purescript
-- | import Prelude
-- |
-- | singleton 1 == 1 :| []
-- | singleton 1 == 1 :| Nil
-- | ```
singleton :: forall f a. Plus f => a -> NonEmpty f a
singleton a = a :| empty

-- | Fold a non-empty structure, collecting results using a binary operation.
-- |
-- | For example:
-- |
-- | ```purescript
-- | foldl1 (+) (1 :| [2, 3]) == 6
-- | ```
foldl1 :: forall f a. Foldable f => (a -> a -> a) -> NonEmpty f a -> a
foldl1 f (a :| fa) = foldl f a fa

-- | Apply a function that takes the `first` element and remaining elements
-- | as arguments to a non-empty container.
-- |
-- | For example, return the remaining elements multiplied by the first element:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping this additional example clarification.

-- |
-- | ```purescript
-- | fromNonEmpty (\x xs -> map (_ * x) xs) (3 :| [2, 1]) == [6, 3]
-- | ```
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
fromNonEmpty f (a :| fa) = a `f` fa

-- | Returns the `alt` (`<|>`) result of:
-- | - The first element lifted to the container of the remaining elements.
-- | - The remaining elements.
-- |
-- | For example:
-- |
-- | ```purescript
-- | import Data.Maybe(Maybe(..))
-- |
-- | oneOf (1 :| Nothing) == Just 1
-- | oneOf (1 :| Just 2) == Just 1
-- |
-- | oneOf (1 :| [2, 3]) == [1,2,3]
-- | ```
oneOf :: forall f a. Alternative f => NonEmpty f a -> f a
oneOf (a :| fa) = pure a <|> fa

-- | Get the 'first' element of a non-empty container.
-- |
-- | For example:
-- |
Copy link
Member

Choose a reason for hiding this comment

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

I think we typically don't include "For example:" just to save on the editor tooltips so the example is closer to hand. We could go straight to the example:

-- | Get the 'first' element of a non-empty container
-- |
-- | ```purescript
-- | head (1 :| [2, 3]) == 1
-- | ```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was following precedent set by existing docs in this file. Will remove this phrasing from all of them.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I didn't realize. Thank you for removing that.

-- | ```purescript
-- | head (1 :| [2, 3]) == 1
-- | ```
head :: forall f a. NonEmpty f a -> a
head (x :| _) = x

-- | Get everything but the 'first' element of a non-empty container.
-- |
-- | For example:
-- |
-- | ```purescript
-- | tail (1 :| [2, 3]) == [2, 3]
-- | ```
tail :: forall f a. NonEmpty f a -> f a
tail (_ :| xs) = xs

Expand Down