Skip to content

Add append to Data.List.NonEmpty

Motivation

Just as appending together regular lists is a common task, appending non-empty lists is also a common task. Having functions to do so in the prelude would make working with non-empty lists much easier and prevent users from re-implementing the same functions over and over.

Proposal

I propose adding an append function to the Data.List.NonEmpty library. This would include, at minimum, a definition for NonEmpty a -> NonEmpty a -> NonEmpty a. It could also include definitions for NonEmpty a -> [a] -> NonEmpty a and [a] -> NonEmpty a -> NonEmpty a.

Here are sample implementations for each case. They currently have very verbose names because I couldn't think of anything better off the top of my head. I assume the first would simply be named append, but I don't have great ideas for the second and third.

appendNonEmptyNonEmpty :: NonEmpty a -> NonEmpty a -> NonEmpty a
appendNonEmptyNonEmpty (x1 :| xs) ys =
  x1 :| xs ++ toList ys

appendNonEmptyRegular :: NonEmpty a -> [a] -> NonEmpty a
appendNonEmptyRegular (x :| xs) ys =
  x :| xs ++ ys

appendRegularNonEmpty :: [a] -> NonEmpty a -> NonEmpty a
appendRegularNonEmpty [] ys =
  ys
appendRegularNonEmpty (x:xs) ys =
  x :| xs ++ toList ys
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information