ndmitchell / extra

Extra Haskell functions
Other
94 stars 37 forks source link

Add Data.List.NonEmpty.Extra? #44

Closed zliu41 closed 5 years ago

zliu41 commented 5 years ago

I use NonEmpty a lot and these are the functions I find useful:

-- | /O(n)/. Append an element to a list.
(|:) :: [a] -> a -> NonEmpty a
(|:) xs x = foldr cons (pure x) xs

-- | Append a list to a non-empty list.
appendl :: NonEmpty a -> [a] -> NonEmpty a
appendl (x :| xs) l = x :| (xs ++ l)

-- | Append a non-empty list to a list.
appendr :: [a] -> NonEmpty a -> NonEmpty a
appendr l nel = foldr cons nel l

There are certainly other useful functions for NonEmpty (e.g., the counterparts of many functions in Data.List.Extra), but I find these particularly useful since they obviate the need to use the partial fromList. So hopefully this is a reasonable starting point.

ndmitchell commented 5 years ago

Sounds entirely reasonable. PR welcome.