What are practical uses of applicative style?

后端 未结 11 681
误落风尘
误落风尘 2021-01-30 01:32

I am a Scala programmer, learning Haskell now. It\'s easy to find practical use cases and real world examples for OO concepts, such as decorators, strategy pattern etc. Books an

11条回答
  •  有刺的猬
    2021-01-30 02:02

    I think Applicatives ease the general usage of monadic code. How many times have you had the situation that you wanted to apply a function but the function was not monadic and the value you want to apply it to is monadic? For me: quite a lot of times!
    Here is an example that I just wrote yesterday:

    ghci> import Data.Time.Clock
    ghci> import Data.Time.Calendar
    ghci> getCurrentTime >>= return . toGregorian . utctDay
    

    in comparison to this using Applicative:

    ghci> import Control.Applicative
    ghci> toGregorian . utctDay <$> getCurrentTime
    

    This form looks "more natural" (at least to my eyes :)

提交回复
热议问题