How do I flatten a NonEmpty of NonEmptys

Viewed 93
2 Answers

As NonEmpty is a Monad you can use join:

join :: Monad m => m (m a) -> m a

Specialized to NonEmpty:

join :: NonEmpty (NonEmpty a) -> NonEmpty a

You could use sconcat from Data.Semigroup. It combines the elements of a nonempty list using <>, which in this case means concatenation.

(It's found there rather than in Data.List.NonEmpty because it's a method of the Semigroup class.)

Related