I am trying to understand how to use StateT to combine two State state transformers based on comments on my examples of the Scalaz state monad .
I seem to be very close, but I had a problem trying to apply sequence .
import scalaz._ import Scalaz._ import java.util.Random val die = state[Random, Int](r => (r, r.nextInt(6) + 1)) val twoDice = for (d1 <- die; d2 <- die) yield (d1, d2) def freqSum(dice: (Int, Int)) = state[Map[Int,Int], Int]{ freq => val s = dice._1 + dice._2 val tuple = s -> (freq.getOrElse(s, 0) + 1) (freq + tuple, s) } type StateMap[x] = State[Map[Int,Int], x] val diceAndFreqSum = stateT[StateMap, Random, Int]{ random => val (newRandom, dice) = twoDice apply random for (sum <- freqSum(dice)) yield (newRandom, sum) }
So, I got to StateT[StateMap, Random, Int] , which I can deploy with the initial random and empty states of the map:
val (freq, sum) = diceAndFreqSum ! new Random(1L) apply Map[Int,Int]() // freq: Map[Int,Int] = Map(9 -> 1) // sum: Int = 9
Now I would like to generate a list of these StateT and use sequence so that I can call list.sequence ! new Random(1L) apply Map[Int,Int]() list.sequence ! new Random(1L) apply Map[Int,Int]() . But when I try this, I get:
type StT[x] = StateT[StateMap, Random, x] val data: List[StT[Int]] = List.fill(10)(diceAndFreqSum) data.sequence[StT, Int]
Any idea? I can use some help for the last stretch - if possible.
scala scalaz state-monad monad-transformers
huynhjl Oct 16 2018-11-11T00: 00Z
source share