It just appeared to me that there is no way to inject a single value into a Monoid in Data.Monoid
. I hesitate to use Data.Monad.return
for this but was hoping to find something like singleton
for several types.
The Monoid
class does not allow for any kind of injection since the monoid is not any kind of container. Some containers are monoids, and then they will have their own means for injection. For a relatively general injection you can use pure
from Applicative
, or return
from Monad
(the former is more general).
Adding to what others have said: Int
forms a monoid (in several different ways). How would you "inject" a value into Int
? Well, you don't; an Int
is just an Int
. You could maybe use zero or something...?
Now, if something is a container, it forms a monoid. But the monoid bit doesn't help you treat it as a container; you need to try something else for that. Lots of things that aren't containers form monoids.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With