fho
fho

Reputation: 6798

How to inject a value into a Monoid?

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.

Upvotes: 7

Views: 369

Answers (2)

MathematicalOrchid
MathematicalOrchid

Reputation: 62848

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.

Upvotes: 6

augustss
augustss

Reputation: 23014

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).

Upvotes: 15

Related Questions