2010-06-11 45 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") |> ... 
相關問題