Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# standard function that invokes its argument

Tags:

f#

Probably a newbie question, but is there a standard function like

let apply f = f()

in F#?

like image 471
Mikhail Shilkov Avatar asked Sep 18 '25 12:09

Mikhail Shilkov


1 Answers

No, there is not a standard function for this.

In most cases, just calling the function is shorter and more obvious than using apply would be, so I'm not entirely sure how this would be useful:

foo ()
apply foo

Now, you can also write application using |>, but that's not very nice either:

() |> foo

I guess the only place where apply would be useful is:

functions |> List.map apply
functions |> List.map (fun f -> f ()) 

Here, the version without apply is shorter, but I don't think it is worth having a named function in the library just for this one use case.

You could actually use |> here to avoid fun, which makes for a lovely piece of ASCII art :-), but not for something that I would ever want to see in my codebase:

functions |> List.map ((|>) ()) 
like image 105
Tomas Petricek Avatar answered Sep 21 '25 09:09

Tomas Petricek