Scalaz - combining List and State Monad in for comprehension
Asked Answered
R

1

7

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.

Retrogradation answered 11/12, 2012 at 0:1 Comment(0)
J
9

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)].

Jetpropelled answered 11/12, 2012 at 0:23 Comment(3)
Looks like exactly what I'm looking for. But it doesn't change the fact I'm going to have to learn to use state transformers in practice sooner or later ;)Retrogradation
I just realized I already read one of your answers containing a detailed description of traverse and traverseS and even praised you for it. I can't imagine how it didn't stick with me...Retrogradation
If anyone needs to do something similar with Cats rather than Scalaz, note there is no traverseS but I have a working example of it using traverseU here: github.com/benhutchison/gesture/blob/master/core/jvm/src/test/…Repeated

© 2022 - 2024 — McMap. All rights reserved.