When to use scalase monads?
I would like to create a simple computation wrapper. The built-in scala ( TraversableLike
) monads are enough for me . And they already have syntactic sugar. From some scala perspective, collection traits are random monads. And here the monads provided by the scalaz library were provided.
What uses cases from a complex type of classified scalase monads? What functionality is not possible for built-in monads and indicates the need for a scalase?
Some clarifications.
This question is not holy military inheritance and class types. My question is about the infrastructure that the skalaz provides. No class type library is suitable, but this referenced library. This complicates things a little. But it also has a set of useful classes that don't have a match in the scala collection library. Because it is a collection library, not a monadic one. Thus, we are talking about the additional functions provided by scalaz. When does it matter?
source to share
First for a point of terminology: sometimes it is useful to use shorthand, for example " Option
is a monad" but " Option
has a monad instance" or " Option
is monadic" is clearer. Perhaps a little embarrassed to say that Scalaz provides a bunch of monads - that it provides, it is a class type Monad
, and this type of instances of multiple types, including some of his own (eg \/
, Task
etc.) and some of the standard library ( List
, Option
etc.).
So I'm going to answer a question similar to your question: What is the meaning of an explicit type class Monad
over the monadic syntactic sugar provided by the standard library?
One place where explicit representation is Monad
useful is when you want to define your own generic combinators or operations. Suppose I want to write a method addM
that takes two monadic values M[Int]
and adds them to the monad. Easy to write for Option
:
def addM(oa: Option[Int], ob: Option[Int]): Option[Int] = for {
a <- oa
b <- ob
} yield a + b
Or for lists:
def addM(oa: List[Int], ob: List[Int]): List[Int] = for {
a <- oa
b <- ob
} yield a + b
These two implementations obviously have a lot in common, and it would be nice to write one generic implementation that will work in both cases - and for any other monadic type. It is very difficult if we only have the standard textual syntax of the standard text library, and very simple if we have a class of type Monad
.
source to share