if- |
if- case-). , , Maybe .Maybe a, a . Maybe a : Just a ( , ) Nothing (, ). Maybe , a b ( :: a -> b) Maybe a Maybe b ( :: Maybe a -> Maybe b). , f, *:fmap :: (a -> b) -> (f a -> f b)
*fmap: 1)fmap id = id,id, , 2)fmap... []
:: -> , . , , , , . , , , . : fmap .data = deriving Show
data = deriving Show
:: ->
=
:: (Maybe ) -> (Maybe )
= fmap
ghci> (Just )
Just
ghci> Nothing
Nothing
a b ? Maybe (a -> b) Maybe a? , Maybe . Control.Applicative, (<*>).Maybe ( -> ) , ? , , , . - , , Nothing. , := Nothing
= Just
Maybe:ghci> :m + Control.Applicative
ghci> <*> Just
Just
ghci> <*> Nothing
Nothing
ghci> <*> Just
Nothing
ghci> <*> Nothing
Nothing
(<$>), fmap ; 2) ( ), ap (<*>) ( , ).if ( == ) {
if ( == ) {
;
} else if ( == ) {
;
} else if ( == ) {
;
} else {
;
}
} else if ( == ) {
if ( == ) {
;
} else if ( == ) {
;
} else if ( == ) {
;
} else {
;
}
} else {
;
}let = lookup
= lookup
in
lookup Maybe a. , Maybe , Maybe , Maybe . ? -, (<*>).Maybe a. , - (Just a) (Nothing), , , . , , , . (<*>) - :let = lookup
= lookup
in case (, ) of
(Just f, Just g) -> Just (f g )
_ -> Nothing
Maybe- , Maybe- ( ).let = lookup
= lookup
in <*> <*> (Just )
Just .let p = lookup fmt parsers :: Maybe FormatReader
nr = lookup fieldq readers :: Maybe (Int, ValueReader)
sy = lookup symq symmetries :: Maybe Symmetry
p' = uncurry <$> p <*> nr :: Maybe ([String] -> Maybe MatrixData)
d = join $ p' <*> (Just toks) :: Maybe MatrixData
m = MM <$> d <*> sy :: Maybe Matrix
in ...
(<$>) , (<*>). Maybe-. (<$>) pure ( ) Just ( Maybe) (<*>).Maybe, Maybe (. p' ), Maybe (Maybe a). join, Maybe a. join Control.Monad. , (join (Just Nothing) Nothing).Nothing). , Either e, Applicative.Maybe (<*>) , , .
http://s.arboreus.com/2010/10/avoiding-ifs-and-cases-with-applicative.html