Combining Predicates in F#

前端 未结 4 1333
日久生厌
日久生厌 2021-02-18 17:44

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 m

4条回答
  •  独厮守ぢ
    2021-02-18 18:02

    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]
    

提交回复
热议问题