Alternative型クラス
定義
code:hs
class Applicative f => Alternative f where
-- | The identity of '<|>'
empty :: f a
-- | An associative binary operation
(<|>) :: f a -> f a -> f a
-- | One or more.
some v = some_v
where
many_v = some_v <|> pure []
some_v = (fmap (:) v) <*> many_v
-- | Zero or more.
many v = many_v
where
many_v = some_v <|> pure []
some_v = (fmap (:) v) <*> many_v
empty
||と同じ要領で、左がNothingのときだけ右を評価する
code:hs
import Control.Applicative
main = do
print $ Just 1 <|> Nothing -- 左のみ
print $ Nothing <|> Just 2 -- 左→右
Alternative型クラスを実装している型
Maybe
STM
etc.