Skip to content

Commit f0a589c

Browse files
committed
Infix operator (:||) for non-empty cons'
1 parent 6d8e30e commit f0a589c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/Data/List/NonEmpty.purs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Data.List.NonEmpty
77
, singleton
88
, length
99
, cons
10-
, cons'
10+
, (:||), cons'
1111
, snoc
1212
, snoc'
1313
, head
@@ -93,7 +93,7 @@ wrappedOperation
9393
-> NonEmptyList b
9494
wrappedOperation name f (NonEmptyList (x :| xs)) =
9595
case f (x : xs) of
96-
x' : xs' -> NonEmptyList (x' :| xs')
96+
x' : xs' -> x' :|| xs'
9797
L.Nil -> unsafeCrashWith ("Impossible: empty list in NonEmptyList " <> name)
9898

9999
-- | Like `wrappedOperation`, but for functions that operate on 2 lists.
@@ -106,7 +106,7 @@ wrappedOperation2
106106
-> NonEmptyList c
107107
wrappedOperation2 name f (NonEmptyList (x :| xs)) (NonEmptyList (y :| ys)) =
108108
case f (x : xs) (y : ys) of
109-
x' : xs' -> NonEmptyList (x' :| xs')
109+
x' : xs' -> x' :|| xs'
110110
L.Nil -> unsafeCrashWith ("Impossible: empty list in NonEmptyList " <> name)
111111

112112
-- | Lifts a function that operates on a list to work on a NEL. This does not
@@ -123,7 +123,7 @@ fromFoldable = fromList <<< L.fromFoldable
123123

124124
fromList :: forall a. L.List a -> Maybe (NonEmptyList a)
125125
fromList L.Nil = Nothing
126-
fromList (x : xs) = Just (NonEmptyList (x :| xs))
126+
fromList (x : xs) = Just (x :|| xs)
127127

128128
toList :: NonEmptyList ~> L.List
129129
toList (NonEmptyList (x :| xs)) = x : xs
@@ -132,16 +132,18 @@ singleton :: forall a. a -> NonEmptyList a
132132
singleton = NonEmptyList <<< NE.singleton
133133

134134
cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
135-
cons y (NonEmptyList (x :| xs)) = NonEmptyList (y :| x : xs)
135+
cons y (NonEmptyList (x :| xs)) = y :|| x : xs
136136

137137
cons' :: forall a. a -> L.List a -> NonEmptyList a
138138
cons' x xs = NonEmptyList (x :| xs)
139139

140+
infixr 5 cons' as :||
141+
140142
snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
141-
snoc (NonEmptyList (x :| xs)) y = NonEmptyList (x :| L.snoc xs y)
143+
snoc (NonEmptyList (x :| xs)) y = x :|| L.snoc xs y
142144

143145
snoc' :: forall a. L.List a -> a -> NonEmptyList a
144-
snoc' (x : xs) y = NonEmptyList (x :| L.snoc xs y)
146+
snoc' (x : xs) y = x :|| L.snoc xs y
145147
snoc' L.Nil y = singleton y
146148

147149
head :: forall a. NonEmptyList a -> a
@@ -195,18 +197,18 @@ findLastIndex f (NonEmptyList (x :| xs)) =
195197

196198
insertAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
197199
insertAt i a (NonEmptyList (x :| xs))
198-
| i == 0 = Just (NonEmptyList (a :| x : xs))
200+
| i == 0 = Just (a :|| x : xs)
199201
| otherwise = NonEmptyList <<< (x :| _) <$> L.insertAt (i - 1) a xs
200202

201203
updateAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
202204
updateAt i a (NonEmptyList (x :| xs))
203-
| i == 0 = Just (NonEmptyList (a :| xs))
204-
| otherwise = NonEmptyList <<< (x :| _) <$> L.updateAt (i - 1) a xs
205+
| i == 0 = Just (a :|| xs)
206+
| otherwise = (x :|| _) <$> L.updateAt (i - 1) a xs
205207

206208
modifyAt :: forall a. Int -> (a -> a) -> NonEmptyList a -> Maybe (NonEmptyList a)
207209
modifyAt i f (NonEmptyList (x :| xs))
208-
| i == 0 = Just (NonEmptyList (f x :| xs))
209-
| otherwise = NonEmptyList <<< (x :| _) <$> L.modifyAt (i - 1) f xs
210+
| i == 0 = Just (f x :|| xs)
211+
| otherwise = (x :|| _) <$> L.modifyAt (i - 1) f xs
210212

211213
reverse :: forall a. NonEmptyList a -> NonEmptyList a
212214
reverse = wrappedOperation "reverse" L.reverse
@@ -231,7 +233,7 @@ concatMap = flip bind
231233

232234
appendFoldable :: forall t a. Foldable t => NonEmptyList a -> t a -> NonEmptyList a
233235
appendFoldable (NonEmptyList (x :| xs)) ys =
234-
NonEmptyList (x :| (xs <> L.fromFoldable ys))
236+
x :|| (xs <> L.fromFoldable ys)
235237

236238
-- | Apply a function to each element and its index in a list starting at 0.
237239
-- |
@@ -298,7 +300,7 @@ intersectBy = wrappedOperation2 "intersectBy" <<< L.intersectBy
298300

299301
zipWith :: forall a b c. (a -> b -> c) -> NonEmptyList a -> NonEmptyList b -> NonEmptyList c
300302
zipWith f (NonEmptyList (x :| xs)) (NonEmptyList (y :| ys)) =
301-
NonEmptyList (f x y :| L.zipWith f xs ys)
303+
f x y :|| L.zipWith f xs ys
302304

303305
zipWithA :: forall m a b c. Applicative m => (a -> b -> m c) -> NonEmptyList a -> NonEmptyList b -> m (NonEmptyList c)
304306
zipWithA f xs ys = sequence1 (zipWith f xs ys)

0 commit comments

Comments
 (0)