A C# library to write functional code - Part I - Background - Luca Bolognese

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

Luca -

☕ 2 min. read

Other posts in the se­ries:

  • Part I - Background
    • Part II - Tuples

      • Part III - Records
      • Part IV - Type Unions{.}
      • Part V - The Match op­er­a­tor{.} In December (slow time in msft) I de­cided to un­der­stand what func­tional pro­gram­ming is all about. When I say understanding’ I don’t mean just pay­ing lip ser­vice to the main con­cepts by know­ingly men­tion­ing them in ca­sual con­ver­sa­tions (i.e. look at this mem­o­iza­tion, man! or this lambda func­tion is so hot!. I can al­ready do that. I in­tel­lec­tu­ally know what the thing is.

      I wanted to *really* un­der­stand it. For me that means writ­ing plenty of code. I had a medium size ap­pli­ca­tion in my mind that I’ve been want­ing to write for quite some time (stock price, div­i­dends, splits down­load­ing and var­i­ous re­turn cal­cu­la­tions), so I went ahead and wrote it. I also wanted to use C#. It would have been eas­ier in F#, but I work on the C# team and love us­ing our own prod­uct.

      My early at­tempts were un­pleas­ing. I would fall back to my OO back­ground and my func­tional code slowly re­verted to OO code. My way of think­ing about it, even if start­ing with the best in­ten­tions, would go back to: what are the ob­jects, what are their re­spon­si­bil­i­ties and such.

      I needed to force my­self some­how; kind of over­com­pen­sate on the other side. I hit on the idea of prag­mat­i­cally defin­ing func­tional pro­gram­ming and try to limit my­self to the sub­set of lan­guage con­structs in­side my de­f­i­n­i­tion. As a way to de­fine it, I used Chapter 3 of Expert F#. I know, I know, I could have read 1,000s of aca­d­e­mic pa­pers and come up with a meta-analy­sis of all of them that for­mally de­fines what functional pro­gram­ming’ re­ally is. But life is too short. I trusted Don.

      The prob­lem is, sev­eral of the lan­guage con­structs in my small de­f­i­n­i­tion of func­tional pro­gram­ming don’t ex­ist in C#. So I went ahead and cre­ated them. I built a lit­tle li­brary to rep­re­sent them and forced my­self to write code us­ing just this li­brary. It worked.

      In this se­ries of posts I will de­scribe what’s in­side this li­brary. I want to em­pha­size that I built it for ed­u­ca­tional pur­pose only, not for per­for­mance or pro­duc­tion code. Caveat emp­tor.

      My plan is to cover the fol­low­ing:

      1. Tuples
        • Records
          • Type Unions
            • Match
      Let’s see if I can find the time to ac­tu­ally write these posts 🙂
15 Comments

Comments

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

Nope.
But I should have thought of one :)

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.

"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.

Luca Bolognese's WebLog

2008-04-08T17:06:24Z

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

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

Luca Bolognese's WebLog

2008-04-21T13:35:41Z

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

Charlie Calvert's Community Bl

2008-04-23T16:58:14Z

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

Frank Quednau

2008-04-28T11:26:58Z

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!

This is very good stuff. My thing is much simpler.
Thanks for the link.

ABDULLAH MOHAMMED SALEH ALHEMY

2008-05-01T12:32:24Z

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

ABDULLAH MOHAMMED SALEH ALHEMY

2008-05-01T12:58:57Z

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

adamjcooper.com/blog

2008-06-03T15:38:15Z

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

adamjcooper.com/blog

2008-06-03T16:58:29Z

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

Luca Bolognese's WebLog

2008-07-15T05:46:23Z

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

0 Webmentions

These are webmentions via the IndieWeb and webmention.io.