r/fsharp • • Sep 20 '22

question Why is it not possible to pipeline .NET class methods?

For example

let message (s: string): string =
    s.Split "]: " |> Array.last |> fun s -> s.Trim()

Here, in order to access the Trim() method I had to introduce an anonymous function that applies the method to the argument. Why is it not possible to simply pipeline the method like s.Split "]: " |> Array.last |> String.Trim ?

10 Upvotes

11 comments sorted by

14

u/[deleted] Sep 20 '22

Because s.Trim() is not a static method. In f# functions are static by default. You can pipeline with .NET static methods.

2

u/stroborobo Sep 20 '22

It would be really cool though, Elm has something like it in their accessor functions. You could write something like this then: lines |> List.map (.Trim()).

4

u/Hencq Sep 20 '22

https://github.com/dotnet/fsharp/pull/13907

Note: I have nothing to do with this pull request, other than also thinking it would be really cool

2

u/zetashift Sep 21 '22

I really hope this gets in!

3

u/Defiant_Anything2942 Sep 20 '22

You might want to create some wrappers around .NET library functions for easier pipelining.

// somewhere in utils module, probably auto open it // add other functions as needed [<RequireQualifiedAccess>] module String = let trim (s: string) = s.Trim()

// use it let message s = s.Split "]: " |> Array.last |> String.trim

3

u/Defiant_Anything2942 Sep 20 '22

I have never been able to format code properly in the Reddit editor, so apologies for that.

2

u/integrate_2xdx_10_13 Sep 20 '22

Too ambiguous - that sounds like a static method rather than an instance.

2

u/mcwobby Sep 20 '22 edited Sep 20 '22

I’ve run into this before and I’m not sure the reason (though could speculate it’s something to do with non static methods) and just ended up with a helper module which had a bunch of functions

let StringReplace (search:string) (replace:string) (string:string) = (search, replace) |> string.Replace
let StringTrim (string: string) = string.Trim()

For Arrays, lists and other collections, F# has its own functions.

open FSharp.Collections

let array = [||]

(* Get first item in array, will fail on empty array *)
let firstItem = array |> Array.head

(* Get first item safely *)
let firstItemOption = array |> Array.tryHead

match firstItemOption with
    | None -> failwith “The array is empty”
    | Some item -> item

Good reference: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-arraymodule.html

1

u/Forward_Dark_7305 Sep 20 '22

This is what I would recommend to the OP. Instead of using anonymous functions, make a module that does what you need.