在 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
让 mod10 = (%)_ 10
?
推荐答案
你可以定义flip
函数,在point-free风格中很常见:
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屋!