-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add examples #36
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,36 +33,90 @@ import Prim.TypeError (class Warn, Text) | |
|
||
-- | A non-empty container of elements of type a. | ||
-- | | ||
-- | For example: | ||
-- | | ||
-- | ```purescript | ||
-- | import Data.NonEmpty | ||
-- | | ||
-- | nonEmptyArray :: NonEmpty Array Int | ||
-- | nonEmptyArray = NonEmpty 1 [2,3] | ||
-- | | ||
-- | import Data.List(List(..), (:)) | ||
-- | | ||
-- | 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`. | ||
-- | | ||
-- | ```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. | ||
-- | | ||
-- | ```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. | ||
-- | | ||
-- | Deprecated, use 'Data.Semigroup.Foldable.foldl1' instead | ||
-- | | ||
-- | ```purescript | ||
-- | foldl1 (+) (1 :| [2, 3]) == 6 | ||
-- | ``` | ||
foldl1 :: forall f a. Foldable f => Warn (Text "'Data.NonEmpty.foldl1' is deprecated, use 'Data.Semigroup.Foldable.foldl1' instead") => (a -> a -> a) -> NonEmpty f a -> a | ||
foldl1 = Foldable1.foldl1 | ||
|
||
-- | 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
-- | | ||
-- | ```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. | ||
-- | | ||
-- | ```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. | ||
-- | | ||
-- | ```purescript | ||
-- | tail (1 :| [2, 3]) == [2, 3] | ||
-- | ``` | ||
tail :: forall f a. NonEmpty f a -> f a | ||
tail (_ :| xs) = xs | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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