>">>>
PureScriptの場合
code:purs(hs)
class (Applicative m, Bind m) <= Monad m
結果的にはhsと同じモノmrsekut.icon
bindを使っている例
code:hs
-- Maybe
let justInc x = Just (x+1) -- 普通の値を引数に取り、モナドを返す
-- bindがないならこう書いてるだろう
withoutBind = do
x <- Just 3
justInc x
withBind = Just 3 >>= justInc
-- IO
main = do
print =<< return 2
code:hs
(Just 3) >> (Just 4) -- Just 4
(Just 3) >>= \_ -> (Just 4) -- 上と同じ
モナド関数a -> m aの関数合成
普通の関数a -> bの関数合成.のモナド関数版
code:hs
(<=<) :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
f <=< g = (\x -> g x >>= f)
モナド関数a -> m aの関数合成
関数合成>>>のモナド関数版
関連する関数
forM
flatMap:: m a -> (a -> m b) -> m b
code:scala
// flatMap :: m a -> (a -> m b) -> m b
// f :: a -> m b
option.flatMap(f)
option match {
case Some(v) => f(v)
case None => None
}
モナドを自作する
HaskellのMonad型クラスの遍歴