[英]Chaining results of Reader monads in Scala
假設我有一些存儲庫API,其中將交易包裝在(Scalaz)Reader單子中。 現在,我想對結果進行計算,並將結果保存回存儲庫中。 我嘗試了類似的東西:
type UOW[A] = Reader[Transaction, A]
object Record1Repo {
override def findAll: UOW[Seq[Record1]] = Reader(t => {
...
})
}
...
repo.run {
for {
all: Seq[Record1] <- Record1Repo.findAll
record: Record <- all
encoding: Encoding <- Processor.encode(record)
_ <- Record2Repo.save(Record2(encoding))
} yield {
logger.info(s"processed record id=${record.id}")
}}
但是,它失敗了,試圖對record <- all
-all中的結果進行映射是徒勞的。
我對這類函數式編程很陌生,找不到合適的表達方式。 任何建議都歡迎。
它失敗,因為您脫離了Reader
monad。
您從Reader
開始,然后從Seq
提取,因此無法在Reader
結構內的flatMap/map
鏈中進行翻譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.