Applicative型クラス
pure
を実装した型クラス
定義
purs(hs)class Apply f <= Applicative f where
pure :: forall a. a -> f a
Haskellの場合
hsclass (Functor f) => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
f a
はここでは「ファンクター値」ではなく「アプリカティブ値」と呼ぶ
instance例
Maybe
hsinstance Applicative Maybe where
pure = Just
Nothing <*> _ = Nothing
(Just f) <*> something = fmap f something
List
hsinstance Applicative [] where
pure x = [x]
fs <*> xs = [f x | f <- fs, x <- xs]
Either
hsinstance Applicative Either a where
pure = Right
IO
hsinstance Applicative IO where
pure = return
a <*> b = do
f <- a
x <- b
return (f x)
<*>
では↓をやってるんだね

hs-- このdo式全体の結果は`IO 値`
(IO 関数) <*> (IO 値) = do
関数 <- IO 関数
値 <- IO 値
pure (関数 値) -- ←これの結果が`IO 値`になる
(->) r
関連
参考
>Brent さんから Applicative スタイルを習ったのが分かって、微笑ましいです。 ref