Is there a standard way of logically combining predicates in F#? For example, let's say I have isCar x
and isBlue x
then I want something that gives me:
let isBlueCar x = isCar x && isBlue x
But using some sort of composition, rather than invocation, maybe like:
let isBlueCar x = isCar && isBlue
Preferably, that something would be able to accept a large/arbitrary number of predicates.
When two sentences have the same subject, we can combine their predicates using a special word called a conjunction. Conjunctions are words like 'and', 'but', and 'so'. They combine sentences or parts of sentences. To combine predicates, we use the conjunction 'and'.
A predicate is a function that returns a Boolean . For example, to check if an Integer is even we can define the function isEven . scala> def isEven(i: Int) = i % 2 == 0 isEven: (i: Int)Boolean. It behaves as you would expect.
You could define a combinator.
let (<&>) f g = (fun x -> f x && g x)
then do
let isBlueCar = isCar <&> isBlue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With