How to compose bool-returning functions

Viewed 162

I have several filter functions with signature 'a -> bool. I want to create a combined filter that AND's the different filters. I know I can do it like this:

let fCombined x =
    f1 x 
    && f2 x
    && f3 x

Is there any more concise way to directly compose the functions without fully applying them (e.g. by removing x from the definition of fCombined)?

(I know there are other ways that don't use bool, e.g. by using 'a -> 'a option functions and composing them using Option.bind, but that's hardly more concise.)

1 Answers

If you want to lose the argument x, you can make yourself a special operator:

let (&&.) f g x = f x && g x

let fCombined = f1 &&. f2 &&. f3

But I don't think this is the kind of thing to worry about. Having the argument is totally fine.

Related