Question

I had some experience in Haskell and currently learning Scala. Am wondering whether there is something equivalent to Monads in Scala??

Was it helpful?

Solution

You probably want to check out scalaz; it's been strongly influenced by Haskell. Indeed, it has often been asked of one of the prime contributors why they aren't just using Haskell, as they seem to like it so much!

Scalaz makes heavy use of implicits in order to decorate structures with their monads. For example:

val fibs = (0, 1).iterate[Stream]( i => i._2 -> (i._2 + i._1) ).map(_._1)
println( fibs.take(10) )

OTHER TIPS

I think is worth noting that Scala's "for-comprehension" is equivalent to Haskell's monadic "do"

Both Option and List are monads. I also believe that Either's left and right projections are also monads.

There is no explicit concept of monad in Scala standard library (there is no appropriate trait/class or typeclass).

Scala deals with this in kind of an ad-hoc manner, see Scala by example's section on for comprehensions for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top