@@ -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
6969import Data.List.Types (NonEmptyList (..))
7070import Data.Maybe (Maybe (..), fromMaybe , maybe )
7171import Data.NonEmpty ((:|))
72+ import Data.NonEmpty (NonEmpty (..))
7273import Data.NonEmpty as NE
7374import Data.Semigroup.Traversable (sequence1 )
7475import Data.Tuple (Tuple (..), fst , snd )
@@ -93,7 +94,7 @@ wrappedOperation
9394 -> NonEmptyList b
9495wrappedOperation 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
107108wrappedOperation2 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
124125fromList :: forall a . L.List a -> Maybe (NonEmptyList a )
125126fromList L.Nil = Nothing
126- fromList (x : xs) = Just (NonEmptyList ( x :| xs) )
127+ fromList (x : xs) = Just (x :|| xs)
127128
128129toList :: NonEmptyList ~> L.List
129130toList (NonEmptyList (x :| xs)) = x : xs
@@ -132,16 +133,18 @@ singleton :: forall a. a -> NonEmptyList a
132133singleton = NonEmptyList <<< NE .singleton
133134
134135cons :: 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
137138cons' :: 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
140143snoc :: 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
143146snoc' :: 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
145148snoc' L.Nil y = singleton y
146149
147150head :: forall a . NonEmptyList a -> a
@@ -195,18 +198,18 @@ findLastIndex f (NonEmptyList (x :| xs)) =
195198
196199insertAt :: forall a . Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a )
197200insertAt 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
201204updateAt :: forall a . Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a )
202205updateAt 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
206209modifyAt :: forall a . Int -> (a -> a ) -> NonEmptyList a -> Maybe (NonEmptyList a )
207210modifyAt 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
211214reverse :: forall a . NonEmptyList a -> NonEmptyList a
212215reverse = wrappedOperation " reverse" L .reverse
@@ -231,7 +234,7 @@ concatMap = flip bind
231234
232235appendFoldable :: forall t a . Foldable t => NonEmptyList a -> t a -> NonEmptyList a
233236appendFoldable (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
299302zipWith :: forall a b c . (a -> b -> c ) -> NonEmptyList a -> NonEmptyList b -> NonEmptyList c
300303zipWith 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
303306zipWithA :: forall m a b c . Applicative m => (a -> b -> m c ) -> NonEmptyList a -> NonEmptyList b -> m (NonEmptyList c )
304307zipWithA f xs ys = sequence1 (zipWith f xs ys)
0 commit comments