Home > Enterprise >  How do I flatten a NonEmpty of NonEmptys
How do I flatten a NonEmpty of NonEmptys

Time:09-17

Data.List.NonEmpty

https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-List-NonEmpty.html

How do I flatten a NonEmpty of NonEmptys -- staying within NonEmpty? IOW I do not want to convert to regular list and do a concat and then convert back to NonEmpty.

CodePudding user response:

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

CodePudding user response:

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