Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compose bool-returning functions

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.)

like image 513
cmeeren Avatar asked Dec 19 '25 20:12

cmeeren


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.

like image 122
Fyodor Soikin Avatar answered Dec 21 '25 11:12

Fyodor Soikin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!