[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!

4 Likes

I’ve just released list1-0.1.0, which adds some new functions and some documentation.

5 Likes

Do operations such as deleteBy return Nothing when they fail to find the element, or only when the resulting list would have been empty?

The only mode of “failure” that this library captures is the shrinking of a NonEmpty list to a possibly-empty one, within the Maybe monad. So deleteBy does not silently fail if there is no matching element, it will simply not delete anything.

1 Like