9

Option monad turns pretty much a facility some times in Scala. Are there any other monads in Scala standard library?

Ivan
  • 63,011
  • 101
  • 250
  • 382

3 Answers3

12

Luigi's answer is correct, but not very informative, IMHO.

All collections can implement the monad interface, but the signature for flatMap in them is not a monad's flatMap. They'll act like monads most of the time, though. Almost all of the classes listed by Luigi are related to collections.

LeftProject and RightProject refers to Either. Basically, Either is not a monad, but if you "project" one of the sides, then that side acts pretty much like an Option monad.

Parser is a monad, which forms the basis of parser combinators.

I admit I do not recognize ControlContext. I wonder if it is related to continuations (which are monads as well).

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 1
    What do you intend by "the signature for flatMap in them is not a monad's flatMap" ? Are you referring to the implicit machinery ? – paradigmatic Feb 13 '12 at 11:02
  • 1
    @paradigmatic, yes. See the Tony Morris rant [here](http://stackoverflow.com/questions/1722726/is-the-scala-2-8-collections-library-a-case-of-the-longest-suicide-note-in-hist/1735694#1735694). – missingfaktor Feb 13 '12 at 11:48
11

You can work this out by looking up the tell-tale flatMap in the API index. It gives:

FilterMonadic 
Stream 
StreamWithFilter 
TraversableMethods 
Iterator 
ParIterableLike 
ParIterableLike 
ParIterableViewLike 
TraversableLike 
WithFilter 
MonadOps 
TraversableProxyLike 
TraversableViewLike 
LeftProjection 
RightProjection 
Option 
WithFilter 
Responder 
Zipped 
ControlContext
Parser 
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
1

Here are the links to three sources files from Scalaz:

Take a look at instance declarations. That might give you an idea about what types from the standard library satisfy the monadic interface.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365