Skip to content

Commit e64a80f

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

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/Data/List/NonEmpty.purs

+18-15
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
@@ -69,6 +69,7 @@ import Data.List as L
6969
import Data.List.Types (NonEmptyList(..))
7070
import Data.Maybe (Maybe(..), fromMaybe, maybe)
7171
import Data.NonEmpty ((:|))
72+
import Data.NonEmpty (NonEmpty(..))
7273
import Data.NonEmpty as NE
7374
import Data.Semigroup.Traversable (sequence1)
7475
import Data.Tuple (Tuple(..), fst, snd)
@@ -93,7 +94,7 @@ wrappedOperation
9394
-> NonEmptyList b
9495
wrappedOperation name f (NonEmptyList (x :| xs)) =
9596
case f (x : xs) of
96-
x' : xs' -> NonEmptyList (x' :| xs')
97+
x' : xs' -> x' :|| xs'
9798
L.Nil -> unsafeCrashWith ("Impossible: empty list in NonEmptyList " <> name)
9899

99100
-- | Like `wrappedOperation`, but for functions that operate on 2 lists.
@@ -106,7 +107,7 @@ wrappedOperation2
106107
-> NonEmptyList c
107108
wrappedOperation2 name f (NonEmptyList (x :| xs)) (NonEmptyList (y :| ys)) =
108109
case f (x : xs) (y : ys) of
109-
x' : xs' -> NonEmptyList (x' :| xs')
110+
x' : xs' -> x' :|| xs'
110111
L.Nil -> unsafeCrashWith ("Impossible: empty list in NonEmptyList " <> name)
111112

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

124125
fromList :: forall a. L.List a -> Maybe (NonEmptyList a)
125126
fromList L.Nil = Nothing
126-
fromList (x : xs) = Just (NonEmptyList (x :| xs))
127+
fromList (x : xs) = Just (x :|| xs)
127128

128129
toList :: NonEmptyList ~> L.List
129130
toList (NonEmptyList (x :| xs)) = x : xs
@@ -132,16 +133,18 @@ singleton :: forall a. a -> NonEmptyList a
132133
singleton = NonEmptyList <<< NE.singleton
133134

134135
cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
135-
cons y (NonEmptyList (x :| xs)) = NonEmptyList (y :| x : xs)
136+
cons y (NonEmptyList (x :| xs)) = y :|| x : xs
136137

137138
cons' :: forall a. a -> L.List a -> NonEmptyList a
138-
cons' x xs = NonEmptyList (x :| xs)
139+
cons' x xs = NonEmptyList (NonEmpty x xs)
140+
141+
infixr 5 cons' as :||
139142

140143
snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
141-
snoc (NonEmptyList (x :| xs)) y = NonEmptyList (x :| L.snoc xs y)
144+
snoc (NonEmptyList (x :| xs)) y = x :|| L.snoc xs y
142145

143146
snoc' :: forall a. L.List a -> a -> NonEmptyList a
144-
snoc' (x : xs) y = NonEmptyList (x :| L.snoc xs y)
147+
snoc' (x : xs) y = x :|| L.snoc xs y
145148
snoc' L.Nil y = singleton y
146149

147150
head :: forall a. NonEmptyList a -> a
@@ -195,18 +198,18 @@ findLastIndex f (NonEmptyList (x :| xs)) =
195198

196199
insertAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
197200
insertAt i a (NonEmptyList (x :| xs))
198-
| i == 0 = Just (NonEmptyList (a :| x : xs))
201+
| i == 0 = Just (a :|| x : xs)
199202
| otherwise = NonEmptyList <<< (x :| _) <$> L.insertAt (i - 1) a xs
200203

201204
updateAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
202205
updateAt i a (NonEmptyList (x :| xs))
203-
| i == 0 = Just (NonEmptyList (a :| xs))
204-
| otherwise = NonEmptyList <<< (x :| _) <$> L.updateAt (i - 1) a xs
206+
| i == 0 = Just (a :|| xs)
207+
| otherwise = (x :|| _) <$> L.updateAt (i - 1) a xs
205208

206209
modifyAt :: forall a. Int -> (a -> a) -> NonEmptyList a -> Maybe (NonEmptyList a)
207210
modifyAt i f (NonEmptyList (x :| xs))
208-
| i == 0 = Just (NonEmptyList (f x :| xs))
209-
| otherwise = NonEmptyList <<< (x :| _) <$> L.modifyAt (i - 1) f xs
211+
| i == 0 = Just (f x :|| xs)
212+
| otherwise = (x :|| _) <$> L.modifyAt (i - 1) f xs
210213

211214
reverse :: forall a. NonEmptyList a -> NonEmptyList a
212215
reverse = wrappedOperation "reverse" L.reverse
@@ -231,7 +234,7 @@ concatMap = flip bind
231234

232235
appendFoldable :: forall t a. Foldable t => NonEmptyList a -> t a -> NonEmptyList a
233236
appendFoldable (NonEmptyList (x :| xs)) ys =
234-
NonEmptyList (x :| (xs <> L.fromFoldable ys))
237+
x :|| (xs <> L.fromFoldable ys)
235238

236239
-- | Apply a function to each element and its index in a list starting at 0.
237240
-- |
@@ -298,7 +301,7 @@ intersectBy = wrappedOperation2 "intersectBy" <<< L.intersectBy
298301

299302
zipWith :: forall a b c. (a -> b -> c) -> NonEmptyList a -> NonEmptyList b -> NonEmptyList c
300303
zipWith f (NonEmptyList (x :| xs)) (NonEmptyList (y :| ys)) =
301-
NonEmptyList (f x y :| L.zipWith f xs ys)
304+
f x y :|| L.zipWith f xs ys
302305

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

0 commit comments

Comments
 (0)