简体   繁体   中英

ignore :: Applicative f => f a -> f ()

I need any function with this signature :

ignore :: Applicative f => f a -> f ()

Can someone point me to the right direction ?

Thanks!

Like @Amadan said in the comments, you can easily find answers to this kind of questions by using the Hoogle search engine. Nevertheless, here are a few ways you can do this:

  1. You can observe that what you need is a mapping to a constant value ( () ). In Haskell, that's const () . All you have to do now is modify this function so that it will affect the value inside an (applicative) functor. And what value makes a function into a function on functors? fmap . Put all of this together, and you get solution #1: fmap $ const () .

  2. There's a function in the Prelude that takes a functor and replaces its value with another: (<$) :: Functor f => a -> fb -> fa . Here, a is () , so you get solution #2: () <$ .

  3. Use Hoogle, and you get void from Data.Functor and Control.Monad . This function is also useful when you have a monadic action and want to ignore its result by switching it out with () .

  4. Write it out the long-winded way with a lambda (as you did in the comments): ignore f = (\\_ -> ()) <$> f , or just ignore = fmap $ \\_ -> ()

How about turning fa into fb for an Applicative f , do you know of anything that can help you with that?

Something that changes the value "on the inside" while keeping the "outside" intact?

Hint: Applicatives are Functors too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM