Purescript Eff Monad:使用非原生计算效果

时间:2017-04-11 11:14:56

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

我希望能够写

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

1 个答案:

答案 0 :(得分:1)

您可以在MaybeT之上使用ReaderTEff 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)