Haskell語言學習筆記(82)Extensible effects

安裝 extensible-effects

$ cabal install extensible-effects
Installed extensible-effects-3.0.0.0

Extensible effects

Extensible effects 是一種代替 Monad Transformers(轉換器)的方法。ui

module Control.Eff.QuickStart
tooBig :: Member (Exc String) r => Int -> Eff r Int
tooBig i = do
  when (i > 100) $ throwError $ show i
  return i

runTooBig :: Int -> Either String Int
runTooBig i = run . runError $ tooBig i
Prelude> :m +Control.Eff.QuickStart
Prelude Control.Eff.QuickStart> runTooBig 1
Right 1
Prelude Control.Eff.QuickStart> runTooBig 200
Left "200"
相關文章
相關標籤/搜索