snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
155 stars 64 forks source link

NonNullToNonEmpty #184

Closed MaxGabriel closed 4 years ago

MaxGabriel commented 4 years ago

Mono-traversable has a nice interface for protecting against empty lists, but occasionally you do need to interface with something that asks for NonEmpty, such as persistent-qq's %{} value replacement.

Do you think adding a function to go from nonNullToNonEmpty would be reasonable? Something like:

nonNullToNonEmpty :: NonNull [a] -> NE.NonEmpty a
nonNullToNonEmpty nonNull = NE.fromList (toList nonNull)
snoyberg commented 4 years ago

Seems reasonable to me. What about upgrading to nonNullToNonEmpty :: Foldable f => NonNull (f a) -> NE.NonEmpty a, which I think should be fine.

MaxGabriel commented 4 years ago

I'm ok with that. I haven't looked at mono-traversable in awhile, but would it be better to use MonoFoldable or just regular Foldable?

toNonEmpty :: MonoFoldable mono => NonNull mono -> NE.NonEmpty (Element mono)
toNonEmpty = NE.fromList . otoList
snoyberg commented 4 years ago

Good point, that sounds better.