[ANN] list1 -- Utilities for NonEmpty lists

The list1 package offers an alternative interface to Data.List.NonEmpty. Most notably are the pattern synonyms

type List1 = NonEmpty

-- | Match a singleton 'List1'.
pattern Sole :: x -> List1 x
pattern Sole x = x :| []

-- | Match a 'List1' of length at least 2.
pattern (:||) :: x -> List1 x -> List1 x
pattern x :|| y <- (x :| (list1 -> Just y))
  where
    x :|| ~(y :| ys) = x :| (y : ys)

{-# COMPLETE Sole, (:||) #-}

-- | Isomorphic to '(:|)', but instead with a 'Maybe' 'List1'.
pattern (:?) :: x -> Maybe (List1 x) -> List1 x
pattern x :? y <- (x :| ~(list1 -> y))
  where
    x :? y = maybe (Sole x) (x :||) y

and extensive use of Maybe in the result types to facilitate working with possibly-empty results without leaving the Maybe monad. Documentation sold separately!

2 Likes