ax1mx2
ax1mx2

Reputation: 704

Not Function And Function Composition in F#

Is it possible it F# to have function composition between Operators.Not and some standard .NET function, for instance String.IsNullOrEmpty?

In other words, why is the following lambda expression unacceptable:

(fun x -> not >> String.IsNullOrEmpty)

Upvotes: 1

Views: 392

Answers (2)

Kwang Yul Seo
Kwang Yul Seo

Reputation: 791

If you want to use argument x, you can use pipe operator |> instead of function composition operators (<< or >>).

fun x -> x |> String.IsNullOrEmpty |> not

But point-free style with function composition is generally preferred.

Upvotes: 1

Tomas Petricek
Tomas Petricek

Reputation: 243041

The >> function composition works the other way round - it passes the result of the function on the left to the function on the right - so your snippet is passing bool to IsNullOrEmpty, which is a type error. The following works:

(fun x -> String.IsNullOrEmpty >> not)

Or you can use reversed function composition (but I think >> is generally preferred in F#):

(fun x -> not << String.IsNullOrEmpty)

Aside, this snippet is creating a function of type 'a -> string -> bool, because it is ignoring the argument x. So I suppose you might actually want just:

(String.IsNullOrEmpty >> not)

Upvotes: 8

Related Questions