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:
- No.
- 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") |> ...
精彩评论