Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Predicates in F#

Tags:

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.

like image 804
GregRos Avatar asked Jun 01 '12 16:06

GregRos


People also ask

How do you combine two 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'.

What is predicate in Scala?

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.


1 Answers

You could define a combinator.

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

then do

let isBlueCar = isCar <&> isBlue 
like image 72
Daniel Avatar answered Oct 31 '22 03:10

Daniel