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.
I'm not sure I understand exactly what you're looking for, but it sounds like you want something more like traverse
here (where traverse
is a more general version of Haskell's mapM
):
import scalaz._, Scalaz._
def adder(i: Int) = State[String, Int](str => (str + i.toString + " ", i))
List(1, 2, 3).traverseS(adder)("start: ")._1
This will print the following, as expected:
res0: String = "start: 1 2 3 "
Note that I'm using traverseS
(where the S
stands for State
) to avoid having to write out the rather messy type parameter, but traverse
is more generally useful anytime you want to map a monadic function over something traversable.
I'm happy to give a StateT
example if this isn't what you wanted, but that's going to end up with you having something of type List[(String, Int)]
.
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