I had some experience in Haskell and currently learning Scala. Am wondering whether there is something equivalent to Monads in Scala??
Monad in scala are a category of data types. Informally, anything that has a type parameter, a constructor that takes an element of that type, and a flatMap method (which has to obey certain laws) is a monad. A monad is a mechanism for sequencing computations.
We've used Option as an example above because Scala's Option type is a monad, which we will cover in more detail soon.
Futures can be considered monads if you never construct them with effectful blocks (pure, in-memory computation), or if any effects generated are not considered as part of semantic equivalence (like logging messages).
MORE POWERFUL TYPE SYSTEM. Both Scala and Haskell are statically typed languages, but Haskell's type system is arguably more powerful, in the sense that it provides more guarantees that are checked by the compiler. It also has superior type inference, which means that it places a lower burden on the programmer.
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) )
I think is worth noting that Scala's "for-comprehension" is equivalent to Haskell's monadic "do"
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