Is there something like Scalas _ placeholder in F# lambdas?

I am new to F# and relatively new to functional programming as a whole. I have spent some time on Scala and was wondering if F# has something similar so that i could write a lambda along the lines of:

Array.filter x.EndsWith "x.xml"

Instead of

Array.filter (fun x -> x.EndsWith "x.xml")

No, the object oriented features and the functional programming features in F# aren’t quite as tightly integrated as they are in Scala. In this case, if you find yourself doing the same thing all the time could always write your own helper function

let endsWith (x:string) (y:string) = y.EndsWith(x)

and then do Array.filter (endsWith "x.xml").

Some libraries have made some effort to “F#-ify” some of the heavy-OO standard library stuff you get in .NET. For example, FSharpx.Extras has a String module that has a lot of this stuff built-in:

open FSharpx
Array.filter (String.endsWith "x.xml")

Though I’ve got to say, the Scala way of doing lambdas is pretty neat.

1 Like

I haven’t tested this script yet, but this is my expertise gathering similar scaled down methods using underscore relative to F#'s general systematics.

let  () =
    async {
        let! x = fn()
        
        x = 1
        
        ()
        
        let! _ = "do something" x
        let! _ = x.dosomething
        let! _ = "do something" x

        return _.Task |> Async.AwaitTask
    }
    
    |> Async.RunSynchronously

Thank you a ton! Your answer was extremely helpful.

1 Like