我假设你需要使用 scalaz 7.0.x 和以下导入(查看 scalaz 6.x 的答案历史记录):
import scalaz._
import Scalaz._
State[S, A]
,其中S
是状态的类型,A
是被装饰值的类型。创建状态值的基本语法使用了State[S, A]
函数:// Create a state computation incrementing the state and returning the "str" value
val s = State[Int, String](i => (i + 1, "str"))
在初始值上运行状态计算:
// start with state of 1, pass it to s
s.eval(1)
// returns result value "str"
// same but only retrieve the state
s.exec(1)
// 2
// get both state and value
s(1) // or s.run(1)
// (2, "str")
Function[A, B]
,定义Function[A, State[S, B]]
。使用State
函数...import java.util.Random
def dice() = State[Random, Int](r => (r, r.nextInt(6) + 1))
然后可以使用for/yield
语法来组合函数:
def TwoDice() = for {
r1 <- dice()
r2 <- dice()
} yield (r1, r2)
// start with a known seed
TwoDice().eval(new Random(1L))
// resulting value is (Int, Int) = (4,5)
TwoDice()
状态计算填充列表。val list = List.fill(10)(TwoDice())
// List[scalaz.IndexedStateT[scalaz.Id.Id,Random,Random,(Int, Int)]]
使用 sequence 来获取一个 State[Random, List[(Int,Int)]]
。我们可以提供一个类型别名。
type StateRandom[x] = State[Random,x]
val list2 = list.sequence[StateRandom, (Int,Int)]
// list2: StateRandom[List[(Int, Int)]] = ...
// run this computation starting with state new Random(1L)
val tenDoubleThrows2 = list2.eval(new Random(1L))
// tenDoubleThrows2 : scalaz.Id.Id[List[(Int, Int)]] =
// List((4,5), (2,4), (3,5), (3,5), (5,5), (2,2), (2,4), (1,5), (3,1), (1,6))
或者我们可以使用sequenceU
,它会推断类型:
val list3 = list.sequenceU
val tenDoubleThrows3 = list3.eval(new Random(1L))
// tenDoubleThrows3 : scalaz.Id.Id[List[(Int, Int)]] =
// List((4,5), (2,4), (3,5), (3,5), (5,5), (2,2), (2,4), (1,5), (3,1), (1,6))
下面是一个关于State[Map[Int, Int], Int]
的示例,用来计算上面列表中各个和出现的频率。函数freqSum
计算出投掷的点数总和,并计算出每个和出现的频率。
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 StateFreq[x] = State[Map[Int,Int],x]
// only get the state
tenDoubleThrows2.copoint.traverse[StateFreq, Int](freqSum).exec(Map[Int,Int]())
// Map(10 -> 1, 6 -> 3, 9 -> 1, 7 -> 1, 8 -> 2, 4 -> 2) : scalaz.Id.Id[Map[Int,Int]]
或者更简洁地使用traverseU
来推断类型:
tenDoubleThrows2.copoint.traverseU(freqSum).exec(Map[Int,Int]())
// Map(10 -> 1, 6 -> 3, 9 -> 1, 7 -> 1, 8 -> 2, 4 -> 2) : scalaz.Id.Id[Map[Int,Int]]
State[S,A]
是StateT[Id, S, A]
的类型别名,因此tenDoubleThrows2最终被归类为Id
类型。我使用copoint
将其转换回List
类型。state
,只是试图感受它。stateT
,也许其他人可以展示如何增强StateFreq
或StateRandom
以执行组合计算。我发现两个状态变换器的组合可以像这样结合:def stateBicompose[S, T, A, B](
f: State[S, A],
g: (A) => State[T, B]) = State[(S,T), B]{ case (s, t) =>
val (newS, a) = f(s)
val (newT, b) = g(a) apply t
(newS, newT) -> b
}
这基于 g
是一个一元函数,它接受第一个状态转换器的结果并返回一个状态转换器。然后以下内容将起作用:
def diceAndFreqSum = stateBicompose(TwoDice, freqSum)
type St2[x] = State[(Random, Map[Int,Int]), x]
List.fill(10)(diceAndFreqSum).sequence[St2, Int].exec((new Random(1L), Map[Int,Int]()))
State[Int, _]
单子:val test1 = for {
a <- init[Int]
_ <- modify[Int](_ + 1)
b <- init[Int]
} yield (a, b)
val go1 = test1 ! 0
// (Int, Int) = (0,1)
init
和modify
的例子。经过一番尝试,init[S]
非常方便生成一个State[S,S]
值,但它允许的另一件事是在for循环中访问状态。modify[S]
是在for循环中转换状态的方便方法。因此,上面的示例可以理解为:
a <- init[Int]
: 从一个Int
状态开始,并将其设置为State[Int, _]
单子包装的值,然后将其绑定到a
_ <- modify[Int](_ + 1)
: 增加Int
状态b <- init[Int]
: 取出Int
状态并将其绑定到b
(与a
相同,但现在状态已增加)a
和b
产生一个State[Int, (Int, Int)]
值。在State[S,A]
中,for循环语法已经使得在A
端上操作变得微不足道。 init
、modify
、put
和gets
提供了一些工具来在State[S,A]
中操作S
端。
博客文章的第二个例子被翻译为:
val test2 = for {
a <- init[String]
_ <- modify[String](_ + "1")
b <- init[String]
} yield (a, b)
val go2 = test2 ! "0"
// (String, String) = ("0","01")
这个解释与test1
非常相似。
第三个例子比较棘手,希望有更简单的方法我还没有发现。
type StateString[x] = State[String, x]
val test3 = {
val stTrans = stateT[StateString, Int, String]{ i =>
for {
_ <- init[String]
_ <- modify[String](_ + "1")
s <- init[String]
} yield (i+1, s)
}
val initT = stateT[StateString, Int, Int]{ s => (s,s).pure[StateString] }
for {
b <- stTrans
a <- initT
} yield (a, b)
}
val go3 = test3 ! 0 ! "0"
// (Int, String) = (1,"01")
stTrans
负责转换两个状态(增加和以 "1"
为后缀),并提取String
状态。 stateT
允许我们在任意单子上添加状态转换。 在这种情况下,状态是一个递增的 Int
。 如果我们调用 stTrans ! 0
,我们最终会得到 M[String]
。在我们的例子中,M
是 StateString
,所以最终我们会得到 StateString[String]
,它是 State[String, String]
。stTrans
中提取出Int
状态值。这就是 initT
的作用。 它只是创建一个对象,以一种可以与 stTrans
进行 flatMap 的方式访问状态。test1
和 test2
,就可以避免所有这些笨拙。它们方便地存储了返回元组的 _2
元素中所需的状态。// same as test3:
val test31 = stateT[StateString, Int, (Int, String)]{ i =>
val (_, a) = test1 ! i
for (t <- test2) yield (a, (a, t._2))
}
以下是一个使用State
的非常简单的示例:
让我们定义一个小的“游戏”,其中一些游戏单位正在与BOSS(也是游戏单位)战斗。
case class GameUnit(health: Int)
case class Game(score: Int, boss: GameUnit, party: List[GameUnit])
object Game {
val init = Game(0, GameUnit(100), List(GameUnit(20), GameUnit(10)))
}
当游戏进行时,我们希望跟踪游戏状态,因此让我们定义我们的“动作”与状态单子相关:
让我们狠狠地攻击boss,使他失去10点生命值:health
:
def strike : State[Game, Unit] = modify[Game] { s =>
s.copy(
boss = s.boss.copy(health = s.boss.health - 10)
)
}
而且BOSS也可以反击!当BOSS反击时,每个队友会失去5点生命值
。
def fireBreath : State[Game, Unit] = modify[Game] { s =>
val us = s.party
.map(u => u.copy(health = u.health - 5))
.filter(_.health > 0)
s.copy(party = us)
}
现在我们可以将这些操作组合成“play”:
现在我们可以将这些动作组合成play
:
def play = for {
_ <- strike
_ <- fireBreath
_ <- fireBreath
_ <- strike
} yield ()
当然在现实生活中,游戏会更加动态,但对于我的小例子来说这已经足够了 :)val res = play.exec(Game.init)
println(res)
>> Game(0,GameUnit(80),List(GameUnit(10)))
所以我们几乎打不到这个首领,还有一个单位已经死了,安息吧。
重点在于组合。 State
(只是一个函数S =>(A,S)
)允许您定义产生结果的操作,并且还可以操纵一些状态而不必太了解状态来自哪里。
Monad
部分提供了组合功能,使您的操作可以组合:
A => State[S, B]
B => State[S, C]
------------------
A => State[S, C]
等等,等等。
P.S. 有关get
、put
和modify
之间的区别:
modify
可以被看作是get
和put
的结合:
def modify[S](f: S => S) : State[S, Unit] = for {
s <- get
_ <- put(f(s))
} yield ()
或者简单地说
def modify[S](f: S => S) : State[S, Unit] = get[S].flatMap(s => put(f(s)))
因此,当您使用modify
时,您在概念上使用get
和put
,或者您可以仅使用它们中的一个。
State[S, x]
对象不持有状态,而是后者的转换。只是我认为名称可以选择得更清晰一些。这与你的回答无关,而是关于Scalaz的问题。 - ziggystareval()
函数中使用!
语法是否仍然有效?对我来说,在 scalaz 7 中并没有找到!
。 - David B.!
现在是eval
;~>
现在是exec
。 - huynhjl