22

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.

4 Answers 4

26

You could define a combinator.

let (<&>) f g = (fun x -> f x && g x)

then do

let isBlueCar = isCar <&> isBlue
2
  • 3
    ... and it really is that easy, huh? I love F#.
    – GregRos
    Jun 1, 2012 at 18:50
  • @GregRos - check out FParsec (quanttec.com/fparsec), a beautifully crafted parsing library built using the combinators approach :-) Jun 11, 2012 at 0:47
7
let meetsAll preds = preds |> Seq.fold (fun p q x -> p x && q x) (fun _ -> true)
// or     let meetsAll preds x = preds |> Seq.forall (fun p -> p x)

as in

let isEven x = x%2 = 0
let isDiv5 x = x%5 = 0
let isDiv7 x = x%7 = 0

let div257 = meetsAll [isEven; isDiv5; isDiv7]

for i in 1..100 do
    if div257 i then
        printfn "%d" i

There is no standard library function for it, but there are a plethora of one-liners you can define on your own, as evidenced by the answers here.

5

You could do something like the following:

let predicates = [isCar; isBlue]
let isBlueCar x = predicates |> List.forall (fun predicate -> predicate x)

More generally:

let combinePredicates predicates =
    fun x -> predicates |> List.forall (fun predicate -> predicate x)

let isBlueCar = combinePredicates [isCar;isBlue]
3
  • 1
    Or in point-free syntax: let isBlueCar = (|>) >> flip List.forall predicates
    – Ramon Snir
    Jun 1, 2012 at 16:53
  • @RamonSnir Where is flip defined though? Nov 11, 2017 at 14:36
  • @GoodNightNerdPride I don't think it's built-in, you need to define it yourself: let flip f a b = f b a
    – Ramon Snir
    Nov 11, 2017 at 15:39
2

Is this what you're looking for?

> let (&&<) a b x = a x && b x

val ( &&< ) : ('a -> bool) -> ('a -> bool) -> 'a -> bool

> let isBlueCar = isCar &&< isBlue

val isBlueCar : (int -> bool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.