Tech Off Thread

2 posts

Functional Style Boolean Logic in C#

Back to Forum: Tech Off
  • User profile image
    AceHack

    Are their any issues with this approach?  I'm also trying to follow a very function first style so am I doing that correctly.  Also can anyone see a way to make this shorter.  For instance I hate having to repeat the signatures twice every time.  var will not work here.  I'm trying to do something similar to what Erik did here. 

    http://channel9.msdn.com/shows/Going+Deep/C9-Lectures-Dr-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-4-of-13/ 

    Thanks.

     

        abstract class Bool
        {
            public static readonly Bool True = new True();
            public static readonly Bool False = new False();
    
            public static readonly Func<Bool, Bool> Not =
                new Func<Bool, Bool>(a => a is True ? False : True );
    
            public static readonly Func<Bool, Func<Bool, Bool>> And =
                new Func<Bool, Func<Bool, Bool>>(a => b => a is True ? b : a);
        }
        class True : Bool {}
        class False : Bool {}
    

  • User profile image
    AceHack

    Actually after looking more carefully it looks like the And function might be more like below, which is better?

     

            public static readonly Func<Bool, Func<Bool, Bool>> And =
                new Func<Bool, Func<Bool, Bool>>(a =>
                    a is True ?
                    new Func<Bool, Bool>(b => b) :
                    new Func<Bool, Bool>(_ => False));
    

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.