Skip to content

Infix operator (:||) for NonEmptyList cons' #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Data.List.NonEmpty
, singleton
, length
, cons
, cons'
, (:||), cons'
, snoc
, snoc'
, head
Expand Down Expand Up @@ -93,7 +93,7 @@ wrappedOperation
-> NonEmptyList b
wrappedOperation name f (NonEmptyList (x :| xs)) =
case f (x : xs) of
x' : xs' -> NonEmptyList (x' :| xs')
x' : xs' -> x' :|| xs'
L.Nil -> unsafeCrashWith ("Impossible: empty list in NonEmptyList " <> name)

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

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

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

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

cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
cons y (NonEmptyList (x :| xs)) = NonEmptyList (y :| x : xs)
cons y (NonEmptyList (x :| xs)) = y :|| x : xs

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

infixr 5 cons' as :||

snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
snoc (NonEmptyList (x :| xs)) y = NonEmptyList (x :| L.snoc xs y)
snoc (NonEmptyList (x :| xs)) y = x :|| L.snoc xs y
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could get pattern matching to work, and reuse :|, we could rewrite this (and friends) as:

snoc (x :| xs) y = x :| L.snoc xs y


snoc' :: forall a. L.List a -> a -> NonEmptyList a
snoc' (x : xs) y = NonEmptyList (x :| L.snoc xs y)
snoc' (x : xs) y = x :|| L.snoc xs y
snoc' L.Nil y = singleton y

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

insertAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
insertAt i a (NonEmptyList (x :| xs))
| i == 0 = Just (NonEmptyList (a :| x : xs))
| i == 0 = Just (a :|| x : xs)
| otherwise = NonEmptyList <<< (x :| _) <$> L.insertAt (i - 1) a xs

updateAt :: forall a. Int -> a -> NonEmptyList a -> Maybe (NonEmptyList a)
updateAt i a (NonEmptyList (x :| xs))
| i == 0 = Just (NonEmptyList (a :| xs))
| otherwise = NonEmptyList <<< (x :| _) <$> L.updateAt (i - 1) a xs
| i == 0 = Just (a :|| xs)
| otherwise = (x :|| _) <$> L.updateAt (i - 1) a xs

modifyAt :: forall a. Int -> (a -> a) -> NonEmptyList a -> Maybe (NonEmptyList a)
modifyAt i f (NonEmptyList (x :| xs))
| i == 0 = Just (NonEmptyList (f x :| xs))
| otherwise = NonEmptyList <<< (x :| _) <$> L.modifyAt (i - 1) f xs
| i == 0 = Just (f x :|| xs)
| otherwise = (x :|| _) <$> L.modifyAt (i - 1) f xs

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

appendFoldable :: forall t a. Foldable t => NonEmptyList a -> t a -> NonEmptyList a
appendFoldable (NonEmptyList (x :| xs)) ys =
NonEmptyList (x :| (xs <> L.fromFoldable ys))
x :|| (xs <> L.fromFoldable ys)

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

zipWith :: forall a b c. (a -> b -> c) -> NonEmptyList a -> NonEmptyList b -> NonEmptyList c
zipWith f (NonEmptyList (x :| xs)) (NonEmptyList (y :| ys)) =
NonEmptyList (f x y :| L.zipWith f xs ys)
f x y :|| L.zipWith f xs ys

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