views:

97

answers:

2

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)
+4  A: 

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.

Nathan Sanders
+3  A: 

Use

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

for now. For example

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