@@ -33,36 +33,90 @@ import Prim.TypeError (class Warn, Text)
33
33
34
34
-- | A non-empty container of elements of type a.
35
35
-- |
36
- -- | For example:
37
- -- |
38
36
-- | ```purescript
37
+ -- | import Data.NonEmpty
38
+ -- |
39
+ -- | nonEmptyArray :: NonEmpty Array Int
40
+ -- | nonEmptyArray = NonEmpty 1 [2,3]
41
+ -- |
42
+ -- | import Data.List(List(..), (:))
43
+ -- |
39
44
-- | nonEmptyList :: NonEmpty List Int
40
- -- | nonEmptyList = 0 :| empty
45
+ -- | nonEmptyList = NonEmpty 1 (2 : 3 : Nil)
41
46
-- | ```
42
47
data NonEmpty f a = NonEmpty a (f a )
43
48
44
49
-- | 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
+ -- | ```
45
58
infixr 5 NonEmpty as :|
46
59
47
60
-- | 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
+ -- | ```
48
68
singleton :: forall f a . Plus f => a -> NonEmpty f a
49
69
singleton a = a :| empty
50
70
51
71
-- | 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
+ -- | ```
52
78
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
53
79
foldl1 = Foldable1 .foldl1
54
80
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
+ -- | ```
55
89
fromNonEmpty :: forall f a r . (a -> f a -> r ) -> NonEmpty f a -> r
56
90
fromNonEmpty f (a :| fa) = a `f` fa
57
91
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
+ -- | ```
58
104
oneOf :: forall f a . Alternative f => NonEmpty f a -> f a
59
105
oneOf (a :| fa) = pure a <|> fa
60
106
61
107
-- | Get the 'first' element of a non-empty container.
108
+ -- |
109
+ -- | ```purescript
110
+ -- | head (1 :| [2, 3]) == 1
111
+ -- | ```
62
112
head :: forall f a . NonEmpty f a -> a
63
113
head (x :| _) = x
64
114
65
115
-- | Get everything but the 'first' element of a non-empty container.
116
+ -- |
117
+ -- | ```purescript
118
+ -- | tail (1 :| [2, 3]) == [2, 3]
119
+ -- | ```
66
120
tail :: forall f a . NonEmpty f a -> f a
67
121
tail (_ :| xs) = xs
68
122
0 commit comments