开发者

F#: any way to use member functions as unbound functions?

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 mystri开发者_StackOverflow社区ng = "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)


This is quite similar to a question I asked a few days ago (but your wording is better). The consensus seems to be:

  1. No.
  2. Maybe the syntax 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 as string#Split "foo". I guess it depends how flexibly you define the # syntax.


Use

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

for now. For example

someString |> (fun s -> s.Split "\n") |> ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜