【问题标题】:Purescript Eff Monad: using non-native computational effectsPurescript Eff Monad:使用非原生计算效果
【发布时间】:2017-09-06 17:55:32
【问题描述】:

我希望能够写作

x :: Eff (reader :: Reader Int, maybe :: Maybe) Int
x = do
  config <- ask -- configuration from (Reader Int) monad
  just config -- using (Maybe) Monad

runX :: Int
runX = runPure (runMaybe doIfNothing (runReader 6 x)) -- outputs: 6

使用Eff Monad

使用Eff可以做到这一点吗?

如果不是,我们如何不使用Eff 使其工作?

【问题讨论】:

    标签: functional-programming monads effect purescript purely-functional


    【解决方案1】:

    您可以在Eff 之上使用MaybeTReaderT monad 转换器,但您不能按照上面写的方式匹配两者:

    import Prelude
    import Data.Maybe
    import Control.Monad.Eff
    import Control.Monad.Eff.Class
    import Control.Monad.Eff.Console
    import Control.Monad.Maybe.Trans
    import Control.Monad.Reader.Trans
    
    x :: ReaderT Int (MaybeT (Eff (console :: CONSOLE))) Int
    x = do
      liftEff (log "Asking...")
      config <- ask
      pure config
    
    main :: Eff (console :: CONSOLE) (Maybe Int)
    main = runMaybeT (runReaderT x 6)
    

    【讨论】:

    • 我不喜欢 monad 转换器,我看过一些谈话,其中 Purescript 的 Eff monad 之类的东西与 Reader 之类的 monad 一起使用,但我不知道是否可以在 Purescript 中做,也许我们需要定义一个类似于 Eff 的新 monad。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2021-08-19
    • 1970-01-01
    • 2010-09-10
    相关资源
    最近更新 更多