1

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)
ildjarn
  • 62,044
  • 9
  • 127
  • 211
ax1mx2
  • 654
  • 1
  • 11
  • 23

2 Answers2

8

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)
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
1

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.

Kwang Yul Seo
  • 771
  • 2
  • 7
  • 15