I have been trying to understand the concept of monads in scala. There are different terminologies associated with monads. Monadic typeclasses, monadic type, monadic operation, monadic collection etc. So i have questions like Can a monadic collection support a non-monadic operation? Is it possible to demystify the different terminologies associated with monads in scala?
-
Hundreds of researchers and bloggers and StackOverflow users have spent at least the last decade "demystifying" monads. It's not like there's not enough to read about it. Here is essentially the same question that got >1k upvotes: https://stackoverflow.com/questions/44965/what-is-a-monad . "Is it possible to demystify the different usages on monads?" is really not a good question that could be answered within a single stackoverflow answer. https://stackoverflow.com/help/how-to-ask – Andrey Tyukin Jan 28 '18 at 19:13
-
i got a good answer below for the question i asked about monad terminologies in scala. i updated question details for more clarity – user2715182 Jan 29 '18 at 09:26
1 Answers
Can a monadic collection support a non-monadic operation
A monadic collection must define two methods: pure
and bind
. It must, most importantly, obey the Monad laws of identity (right and left) and associativity.
Such a collection is List[A]
in Scala, which has pure
via it's apply method:
val l: List[Int] = List(1,2,3)
And flatMap
(bind
):
val flat = l.flatMap(i => List(i))
Laws:
Left Identity:
val f: Int => List[Int] = x => List(x + 1)
val x = 1
val left = List(x).flatMap(f)
val right = f(x)
left == right
Right Identity:
val y = List(1)
val left = y.flatMap(List(_))
val right = y
left == right
Assosiativity:
val f: Int => List[Int] = x => List(x + 1)
val g: Int => List[Int] = x => List(x + 2)
val list = List(1,2,3)
val left = list.flatMap(f).flatMap(g)
val right = list.flatMap(x => f(x).flatMap(g))
left == right
As you can see, List[A]
also supports other operations which are not part of the Monad definition, such as map
(which is given for free by the fact List[A]
is a monad, and every monad is also a functor), filter
, foldLeft
, etc.
To answer your question: Yes, a collection with monadic properties can support more than just the operations defined by a monad.
More specifically, we can say that List[A]
is a monadic collection which implements the Monad[List]
typeclass and by that is a monadic type which has monadic operations.

- 146,575
- 32
- 257
- 321