A C# library to write functional code – Part I – Background


Other posts in the series:


  • Part I – Background
  • Part II – Tuples
  • Part III – Records

  • Part IV – Type Unions

  • Part V – The Match operator

  • In December (slow time in msft) I decided to understand what functional programming is all about. When I say ‘understanding’ I don’t mean just paying lip service to the main concepts by knowingly mentioning them in casual conversations (i.e. “look at this memoization, man!” or “this lambda function is so hot!”. I can already do that. I intellectually know what the thing is.


    I wanted to *really* understand it. For me that means writing plenty of code. I had a medium size application in my mind that I’ve been wanting to write for quite some time (stock price, dividends, splits downloading and various return calculations), so I went ahead and wrote it. I also wanted to use C#. It would have been easier in F#, but I work on the C# team and love using our own product.


    My early attempts were unpleasing. I would fall back to my OO background and my functional code slowly reverted to OO code. My way of thinking about it, even if starting with the best intentions, would go back to: what are the objects, what are their responsibilities and such.


    I needed to force myself somehow; kind of overcompensate on the other side. I hit on the idea of pragmatically defining functional programming and try to limit myself to the subset of language constructs inside my definition. As a way to define it, I used Chapter 3 of “Expert F#“. I know, I know, I could have read 1,000s of academic papers and come up with a meta-analysis of all of them that formally defines what ‘functional programming’ really is. But life is too short. I trusted Don.


    The problem is, several of the language constructs in my small definition of functional programming don’t exist in C#. So I went ahead and created them. I built a little library to represent them and forced myself to write code using just this library. It worked.


    In this series of posts I will describe what’s inside this library. I want to emphasize that I built it for educational purpose only, not for performance or production code. Caveat emptor.


    My plan is to cover the following:



    1. Tuples
    2. Records
    3. Type Unions
    4. Match

    Let’s see if I can find the time to actually write these posts 🙂

    Comments (15)

    1. Jacob says:

      Sounds interesting! I really hope this isn’t some subtle April fool’s joke.

    2. lucabol says:

      Nope.

      But I should have thought of one 🙂

    3. Nair says:

      Please do, can’t wait to read. I am a C# programmer as well and love the language and recently I have started reading Robert’s book on F# fundamentals. It is good, but sometimes it just goes over my head. I didn’t get the "Expert F#". I was kind of intimidated by the name.

    4. lucabol says:

      "Expert F#" is a fantastic book as a functional/OO bridge. Don’t mind the "Expert" tag.

      Also the upcoming "F# for scientists" is really good to show practical applications of it.

      I suggest you get them both.

    5. Previous posts: Part I – Background Tuples are a way for you not to name things. In Object Oriented languages

    6. NN says:

      About pattern match.

      Here: http://rsdn.ru/forum/message/2783992.1.aspx, you have some example of pattern matching in C#

      Source code:

      http://files.rsdn.ru/27808/CSharpPatternMatching.zip

    7. Previous posts: Part I – Background Part II – Tuples Now that we know what Tuples are, we can start talking

    8. Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the

    9. Hm,

      as to the pattern matching: Bart De Smet has done a great job on developing pattern match beauty. I link you to his April archive, where most of his work is done:

      http://community.bartdesmet.net/blogs/bart/archive/2008/04.aspx

      Myself, I have worked on a function composition infrastructure inspired by Haskell’s ‘dot’ operator: http://realfiction.net/?q=node/118

      Enjoy!

    10. lucabol says:

      This is very good stuff. My thing is much simpler.

      Thanks for the link.

    11. ABDULLAH MOHAMMED SALEH ALHEMYARI says:

      MY NAME  :ABDULLAH MOHAMMED SALEH ALHEMYARI            / BANK ACCOUNT name:INTERNATIONAL BANK OF YEMEN /  BRANCH: ZUBAIRY  /swift:IBOYYESAXXX  /ACCOUNT no:A/C#:0002-186557-002’/my E-MAIL ADDRESS: BNAFIF88@hotmail.com/COUNTRY :YEMEN ,P.O. BOX: AL ASBAHI POST OFFIEC P.O. BOX :35055 SANA’A , YEMEN

    12. ABDULLAH MOHAMMED SALEH ALHEMYARI says:

      MY NAME  :ABDULLAH MOHAMMED SALEH ALHEMYARI            / BANK ACCOUNT name:INTERNATIONAL BANK OF YEMEN /  BRANCH: ZUBAIRY  /swift:IBOYYESAXXX  /ACCOUNT no:A/C#:0002-186557-002’/my E-MAIL ADDRESS: BNAFIF88@hotmail.com/COUNTRY :YEMEN

    13. The Quest for Quick-and-Easy Class-Based Immutable Value Objects in C# – Part 1: Introduction

    14. The Quest for Quick-and-Easy Immutable Value Objects in C#

    15. Other posts in the series: Part I – Background Part II – Tuples Part III – Records Part IV – Type Unions