user47376
user47376

Reputation: 2273

Purescript Eff Monad: using non-native computational effects

I want to be able to write

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

using the Eff Monad

Is this possible to do using Eff?

If not how can we make it work not using Eff?

Upvotes: 0

Views: 197

Answers (1)

Phil Freeman
Phil Freeman

Reputation: 4169

You can use the MaybeT and ReaderT monad transformers on top of Eff, but you cannot match the two in the way you wrote above:

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)

Upvotes: 1

Related Questions