简体   繁体   English

忽略 :: 应用 f => fa -> f ()

[英]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.就像@Amadan 在评论中所说的那样,您可以使用Hoogle搜索引擎轻松找到此类问题的答案。 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 () .在 Haskell 中,这是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 . fmap Put all of this together, and you get solution #1: fmap $ const () .将所有这些放在一起,您将得到解决方案 #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 . Prelude 中有一个函数接受一个函子并将其值替换为另一个: (<$) :: Functor f => a -> fb -> fa Here, a is () , so you get solution #2: () <$ .这里, a() ,所以你得到解决方案#2: () <$

  3. Use Hoogle, and you get void from Data.Functor and Control.Monad .使用 Hoogle,你会从Data.FunctorControl.Monad获得void This function is also useful when you have a monadic action and want to ignore its result by switching it out with () .当您有一个 monadic 操作并希望通过使用()将其关闭来忽略其结果时,此函数也很有用。

  4. Write it out the long-winded way with a lambda (as you did in the comments): ignore f = (\\_ -> ()) <$> f , or just ignore = fmap $ \\_ -> ()用 lambda 冗长的方式写出来(就像你在评论中所做的那样): ignore f = (\\_ -> ()) <$> f ,或者只是ignore = fmap $ \\_ -> ()

How about turning fa into fb for an Applicative f , do you know of anything that can help you with that?fa变成fb用于Applicative f怎么样,你知道有什么可以帮助你的吗?

Something that changes the value "on the inside" while keeping the "outside" intact?在保持“外部”完整的同时改变“内部”值的东西?

Hint: Applicatives are Functors too.提示:应用程序也是函子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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