Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PureScript モナド変換子 MaybeT 例
module MaybeTExample
( main
) where
import Control.Monad.Maybe.Trans (MaybeT(..), lift, runMaybeT)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Class.Console (log)
import Prelude (Unit, discard, map, pure, unit, void)
-- https://pursuit.purescript.org/packages/purescript-transformers/4.1.0/docs/Control.Monad.Maybe.Trans#t:MaybeT
-- newtype MaybeT m a = MaybeT (m (Maybe a))
-- https://pursuit.purescript.org/packages/purescript-transformers/4.1.0/docs/Control.Monad.Maybe.Trans#v:runMaybeT
-- runMaybeT :: ∀ m a. MaybeT m a → m (Maybe a)
main :: Effect Unit
main =
let
expr1 :: Unit
expr1 = unit
expr1' :: MaybeT Effect Unit
expr1' = pure expr1
expr2 :: Maybe Unit
expr2 = Just unit
expr2' :: MaybeT Effect Unit
expr2' = MaybeT (pure expr2)
expr3 :: Effect Unit
expr3 = log "expr3"
expr3' :: MaybeT Effect Unit
expr3' = lift expr3 -- or MaybeT (map Just expr3)
expr4 :: Effect (Maybe Unit)
expr4 = map Just (log "expr4")
expr4' :: MaybeT Effect Unit
expr4' = MaybeT expr4
block :: MaybeT Effect Unit
block = do
expr1'
expr2'
expr3'
expr4'
main' :: Effect (Maybe Unit)
main' = runMaybeT block
in
void main'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment