A C# library to write functional code - Part I - Background
Luca Bolognese -Other posts in the series:
- Part I - Background
-
- 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:
- Tuples
- Records
- Type Unions
- Match
- Type Unions
- Records
-
Tags
- CSHARP
15 Comments
Comments
Jacob
2008-04-01T15:36:07ZSounds interesting! I really hope this isn’t some subtle April fool’s joke.
lucabol
2008-04-01T15:40:29ZNope.
But I should have thought of one :)
Nair
2008-04-02T13:01:04ZPlease 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.
lucabol
2008-04-02T13:49:46Z“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:24ZPrevious posts: Part I - Background Tuples are a way for you not to name things. In Object Oriented languages
NN
2008-04-12T06:25:56ZAbout 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:41ZPrevious 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:14ZWelcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the
Frank Quednau
2008-04-28T11:26:58ZHm,
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!
lucabol
2008-04-28T13:00:01ZThis is very good stuff. My thing is much simpler.
Thanks for the link.
ABDULLAH MOHAMMED SALEH ALHEMY
2008-05-01T12:32:24ZMY 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:57ZMY 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:15ZThe Quest for Quick-and-Easy Class-Based Immutable Value Objects in C# - Part 1: Introduction
adamjcooper.com/blog
2008-06-03T16:58:29ZThe Quest for Quick-and-Easy Immutable Value Objects in C#
Luca Bolognese's WebLog
2008-07-15T05:46:23ZOther posts in the series: Part I - Background Part II - Tuples Part III - Records Part IV - Type Unions