结合predicates在F# [英] Combining Predicates in F#

查看:159
本文介绍了结合predicates在F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有逻辑地组合predicates在F#中一个标准的方式? 举例来说,假设我有 ISCAR X isBlue X 然后我想要的东西,给我:

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,这东西大概能接受predicates大/任意数量。

Preferably, that something would be able to accept a large/arbitrary number of predicates.

推荐答案

您可以定义一个组合子。

You could define a combinator.

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

然后执行

let isBlueCar = isCar <&> isBlue

这篇关于结合predicates在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆