Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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)
like image 381
gatoatigrado Avatar asked Feb 27 '23 20:02

gatoatigrado


2 Answers

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.

like image 169
Nathan Shively-Sanders Avatar answered Mar 08 '23 05:03

Nathan Shively-Sanders


Use

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

for now. For example

someString |> (fun s -> s.Split "\n") |> ...
like image 25
Brian Avatar answered Mar 08 '23 03:03

Brian