Skip to content

Commit 184e6a6

Browse files
Merge pull request #36 from milesfrain/examples
Add examples
2 parents 9f231ff + 3c4a44a commit 184e6a6

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/Data/NonEmpty.purs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,90 @@ import Prim.TypeError (class Warn, Text)
3333

3434
-- | A non-empty container of elements of type a.
3535
-- |
36-
-- | For example:
37-
-- |
3836
-- | ```purescript
37+
-- | import Data.NonEmpty
38+
-- |
39+
-- | nonEmptyArray :: NonEmpty Array Int
40+
-- | nonEmptyArray = NonEmpty 1 [2,3]
41+
-- |
42+
-- | import Data.List(List(..), (:))
43+
-- |
3944
-- | nonEmptyList :: NonEmpty List Int
40-
-- | nonEmptyList = 0 :| empty
45+
-- | nonEmptyList = NonEmpty 1 (2 : 3 : Nil)
4146
-- | ```
4247
data NonEmpty f a = NonEmpty a (f a)
4348

4449
-- | An infix synonym for `NonEmpty`.
50+
-- |
51+
-- | ```purescript
52+
-- | nonEmptyArray :: NonEmpty Array Int
53+
-- | nonEmptyArray = 1 :| [2,3]
54+
-- |
55+
-- | nonEmptyList :: NonEmpty List Int
56+
-- | nonEmptyList = 1 :| 2 : 3 : Nil
57+
-- | ```
4558
infixr 5 NonEmpty as :|
4659

4760
-- | Create a non-empty structure with a single value.
61+
-- |
62+
-- | ```purescript
63+
-- | import Prelude
64+
-- |
65+
-- | singleton 1 == 1 :| []
66+
-- | singleton 1 == 1 :| Nil
67+
-- | ```
4868
singleton :: forall f a. Plus f => a -> NonEmpty f a
4969
singleton a = a :| empty
5070

5171
-- | Fold a non-empty structure, collecting results using a binary operation.
72+
-- |
73+
-- | Deprecated, use 'Data.Semigroup.Foldable.foldl1' instead
74+
-- |
75+
-- | ```purescript
76+
-- | foldl1 (+) (1 :| [2, 3]) == 6
77+
-- | ```
5278
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
5379
foldl1 = Foldable1.foldl1
5480

81+
-- | Apply a function that takes the `first` element and remaining elements
82+
-- | as arguments to a non-empty container.
83+
-- |
84+
-- | For example, return the remaining elements multiplied by the first element:
85+
-- |
86+
-- | ```purescript
87+
-- | fromNonEmpty (\x xs -> map (_ * x) xs) (3 :| [2, 1]) == [6, 3]
88+
-- | ```
5589
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
5690
fromNonEmpty f (a :| fa) = a `f` fa
5791

92+
-- | Returns the `alt` (`<|>`) result of:
93+
-- | - The first element lifted to the container of the remaining elements.
94+
-- | - The remaining elements.
95+
-- |
96+
-- | ```purescript
97+
-- | import Data.Maybe(Maybe(..))
98+
-- |
99+
-- | oneOf (1 :| Nothing) == Just 1
100+
-- | oneOf (1 :| Just 2) == Just 1
101+
-- |
102+
-- | oneOf (1 :| [2, 3]) == [1,2,3]
103+
-- | ```
58104
oneOf :: forall f a. Alternative f => NonEmpty f a -> f a
59105
oneOf (a :| fa) = pure a <|> fa
60106

61107
-- | Get the 'first' element of a non-empty container.
108+
-- |
109+
-- | ```purescript
110+
-- | head (1 :| [2, 3]) == 1
111+
-- | ```
62112
head :: forall f a. NonEmpty f a -> a
63113
head (x :| _) = x
64114

65115
-- | Get everything but the 'first' element of a non-empty container.
116+
-- |
117+
-- | ```purescript
118+
-- | tail (1 :| [2, 3]) == [2, 3]
119+
-- | ```
66120
tail :: forall f a. NonEmpty f a -> f a
67121
tail (_ :| xs) = xs
68122

0 commit comments

Comments
 (0)