Instead of fmap
, which applies a function to a value-in-a-functor:
而不是fmap,它将函数应用于函数值:
fmap :: Functor f => (a -> b) -> f a -> f b
I needed a function where the functor has a function and the value is plain:
我需要一个函数,其中函子有一个函数,值很简单:
thing :: Functor f => f (a -> b) -> a -> f b
but I can't find one.
但我找不到一个。
What is this pattern called, where I apply a function-in-a-functor (or in an applicative, or in a monad) to a plain value?
这个模式叫做什么,我将函数函数(或者在应用程序中,或者在monad中)应用于普通值?
I've implemented it already, I just don't quite understand what I did and why there wasn't already such a function in the standard libraries.
我已经实现了它,我只是不太明白我做了什么以及为什么标准库中没有这样的功能。
2 个解决方案
#1
19
You don't need Applicative
for this; Functor
will do just fine:
你不需要申请这个; Functor会做得很好:
apply f x = fmap ($ x) f
-- or, expanded:
apply f x = fmap (\f' -> f' x) f
Interestingly, apply
is actually a generalisation of flip
; lambdabot replaces flip
with this definition as one of its generalisations of standard Haskell, so that's a possible name, although a confusing one.
有趣的是,申请实际上是翻转的概括; lambdabot用这个定义取代了flip作为标准Haskell的一个概括,所以这是一个可能的名称,虽然令人困惑。
By the way, it's often worth trying Hayoo (which searches the entirety of Hackage, unlike Hoogle) to see what names a function is often given, and whether it's in any generic package. Searching for f (a -> b) -> a -> f b
, it finds flip
(in Data.Functor.Syntax
, from the functors
package) and ($#)
(from the synthesizer
package) as possible names. Still, I'd probably just use fmap ($ arg) f
at the use site.
顺便说一下,经常值得尝试的是Hayoo(它搜索整个Hackage,不像Hoogle)来查看函数的名称,以及它是否在任何通用包中。搜索f(a - > b) - > a - > f b,它找到flip(在Data.Functor.Syntax中,来自functors包)和($#)(来自合成器包)作为可能的名称。不过,我可能只是在使用网站上使用fmap($ arg)f。
#2
7
As Niklas says, this is application in some applicative functor to a lifted value.
正如尼克拉斯所说,这是应用于一些应用函子,以提升价值。
\f a -> f <*> pure a
:: Applicative f => f (a -> b) -> a -> f b
or more generally (?), using Category (.)
或更一般地(?),使用类别(。)
\f a -> f . pure a
:: (Applicative (cat a), Category cat) => cat b c -> b -> cat a c
#1
19
You don't need Applicative
for this; Functor
will do just fine:
你不需要申请这个; Functor会做得很好:
apply f x = fmap ($ x) f
-- or, expanded:
apply f x = fmap (\f' -> f' x) f
Interestingly, apply
is actually a generalisation of flip
; lambdabot replaces flip
with this definition as one of its generalisations of standard Haskell, so that's a possible name, although a confusing one.
有趣的是,申请实际上是翻转的概括; lambdabot用这个定义取代了flip作为标准Haskell的一个概括,所以这是一个可能的名称,虽然令人困惑。
By the way, it's often worth trying Hayoo (which searches the entirety of Hackage, unlike Hoogle) to see what names a function is often given, and whether it's in any generic package. Searching for f (a -> b) -> a -> f b
, it finds flip
(in Data.Functor.Syntax
, from the functors
package) and ($#)
(from the synthesizer
package) as possible names. Still, I'd probably just use fmap ($ arg) f
at the use site.
顺便说一下,经常值得尝试的是Hayoo(它搜索整个Hackage,不像Hoogle)来查看函数的名称,以及它是否在任何通用包中。搜索f(a - > b) - > a - > f b,它找到flip(在Data.Functor.Syntax中,来自functors包)和($#)(来自合成器包)作为可能的名称。不过,我可能只是在使用网站上使用fmap($ arg)f。
#2
7
As Niklas says, this is application in some applicative functor to a lifted value.
正如尼克拉斯所说,这是应用于一些应用函子,以提升价值。
\f a -> f <*> pure a
:: Applicative f => f (a -> b) -> a -> f b
or more generally (?), using Category (.)
或更一般地(?),使用类别(。)
\f a -> f . pure a
:: (Applicative (cat a), Category cat) => cat b c -> b -> cat a c