Lay González
Lay González

Reputation: 2985

How to make more compact this F# code

I want to go from:

let a = fun x ->
        x
        |> f
        |> g

to something like this:

let a = |> f
        |> g

I tried:

let a = (<|)  f 
              |> g

and similars

Upvotes: 3

Views: 117

Answers (1)

Michael Beidler
Michael Beidler

Reputation: 118

let a = fun x -> x |> f |> g

is equivalent to

let a x = x |> f |> g

It looks like you want to compose two functions f and g to create a new function a. You can use the >> operator to compose functions. You could write:

let a = f >> g

If f and g are generic functions then it will fail to compile due to F# value restrictions. In that case, you need to add type annotations:

let a<'a> : ('a -> 'a) = f >> g

Upvotes: 7

Related Questions