- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法更改的函数返回 Scalaz Reader
,
type Action[A] = Reader[Session, A]
def findAccount(s: String): Action[Account] =
Reader((session: Session) => Account(s))
我想创建一个基于 findAccount(...)
的新函数来返回 ReaderT[Option, Session, A]
type ActionT[A] = ReaderT[Option, Session, A]
def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))
因为最终我想这样做,
def findAccBalT(accountNumber: String) = for {
acc <- findAccountT(accountNumber)
bal <- findBalanceT(acc)
} yield bal
我该如何继续?是否有意义?谢谢
全面披露,
import scalaz._
import Scalaz._
trait Session {
def doSomething(): Unit
}
case class Account(number: String) extends AnyVal
case class Amount(value: Int, currency: String)
case class DBSession() extends Session {
override def doSomething = println("writing to db")
}
type Action[A] = Reader[Session, A]
type ActionT[A] = ReaderT[Option, Session, A]
def findAccount(s: String): Action[Account] =
Reader((session: Session) => Account(s))
def findBalance(account: Account): Action[Amount] =
Reader((session: Session) => Amount(333, "S$"))
// failed
def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))
// failed
def findBalanceT(account: Account): ActionT[Amount] = findBalance(account).map(Option(_))
// failed
def findAccBalT(accountNumber: String) = for {
acc <- findAccountT(accountNumber)
bal <- findBalanceT(acc)
} yield bal
最佳答案
简答:您可以使用 mapK
。
Reader[A]
是 ReaderT[Id, A]
的类型别名,ReaderT
是 Kleisli
的别名。 Id[A]
与 A
相同。
在 Kleisli
ScalaDoc 中我们找到了 mapK
:
def mapK[N[_], C](f: (M[B]) => N[C]): Kleisli[N, A, C]
因为我们知道 Reader[Session, A]
与 Kleisli[Id, Session, A]
相同,所以我们可以使用 mapK
转到 Kleisli[Option, Session, A]
:
import scalaz._, Scalaz._
type Session = String
type Action[A] = Reader[Session, A]
type ActionT[A] = ReaderT[Option, Session, A]
val action: Action[String] = Reader(s => s)
val actionT: ActionT[String] = action mapK Option.apply
关于Scalaz Reader 到 ReaderT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641796/
如何访问 ReaderT 的内部 monad。 在我的例子中,我有类型: newtype VCSSetupAction a = VCSSetupAction (ReaderT (Maybe VCSCo
我正在使用 ReaderT 为这种类型编写 Monad 实例并通过派生 newtype HIO a = HIO {runHIO :: Set HiPermission -> IO a} 我试过这样做
我想这样玩元组和镜头: myfct :: ReaderT (a,b,c,d,e) m a -> ReaderT (a,c,d) m a myfct = zoom ... 能够将输入元组修改为它的子集.
假设我有这样的环境记录: import Control.Monad.IO.Class import Control.Monad.Trans.Reader type RIO env a = Reader
我一直在关注和扩展教程 Write Yourself A Scheme 。我有一个类型 LispVal 包裹在几层 monad 转换器中: import qualified Data.Map as M
看来能够组合不同的 ReaderT 环境会很有用。 例如,通用日志记录工具可能如下所示: logit :: Text -> ReaderT Bool IO () logit str = do deb
我想这样玩元组和镜头: myfct :: ReaderT (a,b,c,d,e) m a -> ReaderT (a,c,d) m a myfct = zoom ... 能够将输入元组修改为它的子集.
假设我有这样的环境记录: import Control.Monad.IO.Class import Control.Monad.Trans.Reader type RIO env a = Reader
我无法更改的函数返回 Scalaz Reader, type Action[A] = Reader[Session, A] def findAccount(s: String): Action[Acc
我基于 ReaderT design pattern 构建了一个项目.我没有使用类型类方法进行依赖注入(inject),而是选择使用简单的处理程序注入(inject)作为函数参数。这部分工作正常,因为
我经常使用以下样板,并希望将其消除。它看起来像这样: type Configured = ReaderT Config doSomething :: Configured IO Data doSome
如何使用 Reader/ReaderT 来询问列表类型,例如[(Int, Int)] 然后在列表 monad 内执行计算(要求的类型)? 下面是我损坏的代码,为了清晰起见,缩短了代码: attempt
我想将 ReaderT 嵌入到另一个 monad 转换器中。我该怎么做呢?下面的示例使用 Scotty但我认为这与任何其他 monad 都是一样的。 {-# LANGUAGE OverloadedSt
我在看the documentation对于Reader monad和 ReaderT单子(monad)变压器。 相关定义如下: newtype ReaderT k r m a :: forall k
通过保持 IORef 来通过异常来维护状态似乎要容易得多。而不是尝试使用 State Monad。下面我们有 2 个替代的 State Monad。一用StateT和另一个 ReaderT IORef
是否可以将以下函数重写为单行函数? action :: NewTenant -> AppM (Either TenantCreationError Tenant) action newTenant =
我正在使用 ReaderT Monad 转换器通过多个执行 IO 的函数从主函数传播配置数据。需要数据的最终函数不执行任何 IO。我有这个工作解决方案: import Control.Monad.Re
我正在设计一个小游戏,它基本上使用 StateT 并且只是更新状态。以下是简化版本: {-# LANGUAGE TemplateHaskell #-} import Control
我试图了解 Servant 的 serveWithContext 函数的用途。文档指出它不是 ReaderT Monad 的替代品,但我不确定它试图解决 ReaderT 尚未解决的问题。 例如,这是来
我想写一个haskell程序来读取配置并做点什么。配置是 Data.Map并将被注入(inject)到 Reader 中。当我们找不到配置项时,应该中断读取。刚刚停止,不需要错误消息。所以我只想要一个
我是一名优秀的程序员,十分优秀!