在F#中使用无点样式的非对称算子的部分函数应用程序? [英] Partial function application for a non-symmetric operator using point-free style in F#?
问题描述
我如何为非对称运算符(例如模数运算符)的第一个参数创建偏函数应用程序,而F#中没有任何参数名称?我的第一次尝试是
How can I create a partial function application for a non-symmetric operator such as the modulus operator with regards to the first argument without any argument names in F#? My first attempt was
let mod10 = (%) 10
当然可以翻译为
mod10(x) = 10 mod x
而不是所需的
mod10(x) = x mod 10
.
当然可以写
let mod10 x = (%)x 10
,但我不想命名该参数,因此是否可以使用一些占位符,例如
let mod10 x = (%)x 10
but I'd like to not have to name the argument so is there some placeholder that can be used, something like
let mod10 = (%)_ 10
?
推荐答案
您可以定义无点样式中常见的flip
函数:
You can define flip
function which is common in point-free style:
let inline flip f x y = f y x
并像这样使用它:
let (%-) = flip (%)
let mod10 = (%-) 10
或直接这样:
let mod10 = flip (%) 10
无点样式并不总是可读(如本例所示),并且在F#编程中并不流行.
Point-free style is not always readable (as in this example) and not popular in F# programming.
这篇关于在F#中使用无点样式的非对称算子的部分函数应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!