C# 我是否正确使用了Lambda微积分项组合子?

C# 我是否正确使用了Lambda微积分项组合子?,c#,linq,linqpad,lambda-calculus,C#,Linq,Linqpad,Lambda Calculus,我是OOP heritage C#开发者,努力尊重函数式编程背后的理论。在下面的代码示例中,我是否正确使用了Lambda演算术语combinator(在funcombinator中) <Query Kind="Program" /> public static class FuncExtensions { public static Func<T, bool> And<T>(this Func<T, bool> thisPredicate

我是OOP heritage C#开发者,努力尊重函数式编程背后的理论。在下面的代码示例中,我是否正确使用了Lambda演算术语combinator(在
funcombinator
中)

<Query Kind="Program" />

public static class FuncExtensions
{
    public static Func<T, bool> And<T>(this Func<T, bool> thisPredicate, Func<T, bool> predicate)
    {
        return a => thisPredicate(a) && predicate(a);
    }

    public static Func<T, bool> AndNot<T>(this Func<T, bool> thisPredicate, Func<T, bool> predicate)
    {
        return a => thisPredicate(a) && !predicate(a);
    }

    public static Func<T, bool> Or<T>(this Func<T, bool> thisPredicate, Func<T, bool> predicate)
    {
        return a => thisPredicate(a) || predicate(a);
    }

    public static Func<T, bool> OrNot<T>(this Func<T, bool> thisPredicate, Func<T, bool> predicate)
    {
        return a => thisPredicate(a) || !predicate(a);
    }
}

public static class FuncCombinator
{
    public static Func<T, bool> Combine<T>(bool @bool = true) { return f => @bool; }
}

void Main()
{
    var data = new[] { 2, 4, 6, 8, 11, 43, 65 };
    var greaterThanFourAndLessThanForty = FuncCombinator.Combine<int>()
        .And<int>(i => i > 4)
        .And<int>(i => i < 40);
    data.Where(greaterThanFourAndLessThanForty).Dump("Greater Than Four and Less Than Forty");

    var notGreaterThanFourAndLessThanForty = FuncCombinator.Combine<int>()
        .AndNot(greaterThanFourAndLessThanForty);
    data.Where(notGreaterThanFourAndLessThanForty).Dump("Not Greater Than Four and Less Than Forty");
}

公共静态类扩展
{
公共静态Func和(此Func thisPredicate,Func predicate)
{
返回a=>thisPredicate(a)&&predicate(a);
}
公共静态Func AndNot(此Func thisPredicate,Func谓词)
{
返回a=>thisPredicate(a)&&!predicate(a);
}
公共静态Func或(此Func thisPredicate,Func predicate)
{
返回a=>thisPredicate(a)| | predicate(a);
}
公共静态Func OrNot(此Func thisPredicate,Func predicate)
{
返回a=>thisPredicate(a)| |!谓词(a);
}
}
公共静态类funcombinator
{
公共静态Func Combine(bool@bool=true){return f=>@bool;}
}
void Main()
{
var数据=新[]{2,4,6,8,11,43,65};
var greaterthanfourandlessthanfour=funcombinator.Combine()
.和(i=>i>4)
.和(i=>i<40);
数据。其中(大于四个小于四十个)。转储(“大于四个小于四十个”);
var notgreaterthanfour=funcombinator.Combine()
.与否(大于或小于40);
数据。其中(不大于四个小于四十个)。转储(“不大于四个小于四十个”);
}

您可能只想使用
True()=>x=>True,而不是
Combine
False()=>x=>False
既然您提到了lambda演算,那么combinator就是没有自由变量的lambda,比如
x=>x
。不确定此定义与FuncCombinator类的关系。