Filter 如何在F#中定义一类带有中缀运算符的布尔函数?

Filter 如何在F#中定义一类带有中缀运算符的布尔函数?,filter,f#,infix-notation,Filter,F#,Infix Notation,也许它已经在F#中实现了 基本上,我想定义一类带有中缀运算符的通用过滤器函数 type Filter<'T> = ('T -> bool) with static member (|*) (f:Filter<'T>) (g:Filter<'T>) = (fun x -> (f x) || (g x)) // OR operator type Filter bool)带有 静态成员(|*)(f:Filter)=(funx->

也许它已经在F#中实现了

基本上,我想定义一类带有中缀运算符的通用过滤器函数

type Filter<'T> = ('T -> bool) with 
     static member (|*) (f:Filter<'T>) (g:Filter<'T>) = (fun x -> (f x) || 
     (g x)) // OR operator
type Filter bool)带有
静态成员(|*)(f:Filter)=(funx->(fx)|
(gx))//或运算符
但这似乎不是正确的语法

由于系统错误而停止。异常:无法执行操作 由于早期错误而完成类型缩写不能包含 2,5类型缩写的增广不能在3,19中包含成员


谢谢

您所定义的是一个,正如错误所示,它既不能有扩充也不能有成员。您可以使用以下方法解决此问题:

type Filter bool)带有
静态成员(|*)(过滤器f、过滤器g)=
过滤器(funx->fx | | gx)//或运算符
当然,您现在需要在布尔运算之前展开谓词函数,然后再次包装组合函数。一个简单的测试

let odd x = x % 2 <> 0
let big x = x > 10
let (Filter f) = Filter odd |* Filter big in [8..12] |> List.filter f
// val it : int list = [9; 11; 12]
设奇数x=x%2 0
让大x=x>10
let(Filter f)=Filter奇数|*Filter big in[8..12]|>List.Filter f
//val-it:int-list=[9;11;12]

将显示错误消息nice@robkuz我为您添加了错误消息
let odd x = x % 2 <> 0
let big x = x > 10
let (Filter f) = Filter odd |* Filter big in [8..12] |> List.filter f
// val it : int list = [9; 11; 12]