serokell / universum

:milky_way: Prelude written in @Serokell
MIT License
176 stars 28 forks source link

Add 'maybeToMonoid'? #52

Closed chshersh closed 7 years ago

chshersh commented 7 years ago

I propose to add next function:

maybeToMonoid :: Monoid m => Maybe m -> m
maybeToMonoid = maybe mempty id

When there was Foldable for Maybe, maybeToMonoid is just fold. But I think that this function is useful. And it's behavior and type is clear from name, also what it does and how also clear from type.

gromakovsky commented 7 years ago

Btw, maybe a id is fromMaybe a.

chshersh commented 7 years ago

@gromakovsky Even better :)

chshersh commented 7 years ago

One more convenient function is maybeWithMonoid :: Monoid m => Maybe a -> (a -> m) -> m. After it maybeToMonoid x becomes just maybeWithMonoid x id.