F#:有什么方法可以将成员函数用作未绑定函数吗?
有没有办法提取成员函数并将它们用作 F# 函数?我希望能够编写以下内容:
mystring |> string.Split '\n' |> Array.filter (string.Length >> (=) 0 >> not)
如果您[让],上面的代码就可以工作
let mystring = "a c\nb\n"
let stringSplit (y:char) (x:string) = x.Split(y)
let stringLength (x:string) = x.Length
mystring |> stringSplit '\n' |> Array.filter (stringLength >> (=) 0 >> not)
Is there a way to extract member functions, and use them as F# functions? I'd like to be able to write the following:
mystring |> string.Split '\n' |> Array.filter (string.Length >> (=) 0 >> not)
The code above works if you [let]
let mystring = "a c\nb\n"
let stringSplit (y:char) (x:string) = x.Split(y)
let stringLength (x:string) = x.Length
mystring |> stringSplit '\n' |> Array.filter (stringLength >> (=) 0 >> not)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与我几天前问的一个问题非常相似(但你的措辞是更好的)。共识似乎是:
string#Split
、"foo"#Split
,或者只是#Split
(类型推断) 将来会添加。但是 Tomas 链接到的 Don Syme 的提案是 2007 年的,所以我不知道我不知道它发生的可能性有多大——我猜,可能与懒惰的特定语法一样可能。编辑:
我猜
"foo"#Split
也可以写成string#Split "foo"
。我想这取决于您定义#
语法的灵活性。This is quite similar to a question I asked a few days ago (but your wording is better). The consensus seems to be:
string#Split
,"foo"#Split
, or just#Split
(type-inferred) will be added in the future. But Don Syme's proposal that Tomas linked to was from 2007, so I don't know how likely it is to happen--probably about as likely as a specific syntax for laziness, I'd guess.Edit:
I guess
"foo"#Split
could also be written asstring#Split "foo"
. I guess it depends how flexibly you define the#
syntax.用一下
暂时 。例如
Use
for now. For example