2010-06-11 42 views
4

有没有办法提取成员函数,并将它们用作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) 

回答

5

这是非常相似a question I asked a few days ago(但你的措辞更好)。的共识似乎是:

  1. 也许语法string#Split"foo"#Split,或者只是#Split(类型推断)将在未来加入。但Tomas所连接的Don Syme's proposal是从2007年开始的,所以我不知道它会发生多大的可能性 - 我猜可能与懒惰的特定语法差不多。

编辑:

我想"foo"#Split也可以写成string#Split "foo"。我想这取决于你如何灵活地定义#语法。

3

使用

(fun x -> x.Member ...) 

现在。例如

someString |> (fun s -> s.Split "\n") |> ... 
相关问题