I am planning to start using Monadic style in my Scala code for, amongst others, threading state. Here's a simplified example of combining 3 monadic functions (and caring only about the side effects)
import scalaz._
import Scalaz._
object MonadTest {
def adder(i: Int) = State[String, Int] ({str: String => (str + i.toString + " ", i) })
val oneTwoThreeMonad = for {
m1 <- adder(1)
m2 <- adder(2)
m3 <- adder(3)
} yield m3
oneTwoThreeMonad("start: ")._1 //String = "start: 1 2 3 "
}
This all is pretty self-explanatory and works as expected. But for this approach to be really useful to me I would like to be able to combine it with List
for-comprehension. Here's a bit of (not working) code to show what I mean:
val list = List(1, 2, 3)
val oneTwoThreeBis = for {
i <- list
mx <- adder(i)
} yield mx
Basically I would like to be able to combine monads based on arguments from a List
- run the monadic function on each of the elements of the list
and accumulate the side-effects as I go. I understand the example syntax doesn't work and I see why it doesn't - I'm just looking for a clean, elegant equivalent.
I am pretty sure it is possible to achieve this using scalaz monad transformers, more specifically with StateT
but I'm not really sure how one would go about doing it.
PS. I'm using Scalaz 7.0-M3, so the syntax might be a little different from the most common 6.x.