[英]A common pattern involving composition of functions (\a b -> f (g a) (g b))
The composition of f and g that looks like f和g的组成看起来像
f :. g = \a b -> f (g a) (g b)
is a pattern I find very often in my code. 是我在代码中经常发现的模式。 It is similar to unary function composition, only
f
is binary and I want g
applied to both arguments before they are passed to f
. 它类似于一元函数组合,只有
f
是二元的,我想在传递给f
之前将g
应用于两个参数。
When I ask lambdabot to convert this to point-free form, I get the weird incantation 当我要求lambdabot将其转换为无点形式时,我得到了奇怪的咒语
flip ((.) . f . g) g
which I'd rather not have in my code, so I end up just writing out the pattern explicitly. 在我的代码中我不想拥有它,所以我最终只是明确地写出了模式。
Is there a commonly accepted way of writing a combinator for this situation? 是否有一种普遍接受的方法来为这种情况编写组合子? Or am I weird for finding myself in this situation quite a lot?
或者我在这种情况下发现自己很奇怪?
I don't have an actual example of when I use this on hand right now since I have never thought to ask here when I've needed it, but one could imagine writing the euclidean distance formula very neatly with it, like so: 我现在没有一个实际的例子,因为当我需要时,我从未想过要问这里,但可以想象用它非常整齐地写出欧几里德距离公式,就像这样:
distance = sqrt . (+) :. (^2)
This function is called on
in the Data.Function
module. 该功能被称为
on
的在Data.Function
模块。
It's often used infix, such as sqrt . (+) `on` (^2)
它经常使用中缀,例如
sqrt . (+) `on` (^2)
sqrt . (+) `on` (^2)
. sqrt . (+) `on` (^2)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.