Simple Functional Programming in C#

Some of the greatest crimes in modern computing are committed in the name of good object-orientated abstractions. Over time, a code base gets so polluted with abstract classes, factories and heavy dependencies on frameworks that a lot of it ends up serving as boilerplate to work around the rest. I’ve spent the last few months learning Haskell (A pure functional language) and Go (An imperative language that allows OO and functional styles) and it’s led me to the conclusion that John Carmack is right, sometimes all you really need is a function.

Go’s approach to OO struck me as very interesting because instead of complex class hierarchies they took a very simple approach and allowed you to associate a function with a type. There’s no this keyword, you assign the type a name and use that to access it’s properties. Here’s a very simple example:

Note: Unused variables are compile-time errors in Go, hence why this is much simpler than the upcoming C# examples.

We can do something similar in C#. Here’s a very simple OO example in C#. In pure OO, an object is a bundle of state and methods that operate on that state so I’m going with a contrived sample that demonstrates this.

Now lets look at the functional solution:

Note on line 2 we’ve given Tuples of type <string, string, int> the alias of Person. Since System.Tuple is a class, we create our Person tuple almost exactly the same way as our OO solution. The biggest difference is that there is no maintained state. Our CompareAge function takes two Person tuples and computes the required value. There’s also a lot less worrying about encapsulation, since we can explicitly see that our people tuples only exist in the scope where we create them.

I’ll expand on this post in the future with some more example of how aliasing types in C# can be used to write concise functional code. If you’d like to try this out, have a go at aliasing a list of Persons and writing a function that iterates over them and returns the total age of everyone (this is a classic example of a functional fold operation!)

I’m trying a more lean, agile approach to blogging because the tax on adding links and sources with my limited free time is too great and I don’t want to stop completely. If you want me to expand on anything in this post feel free to leave a comment or drop me a line and I’ll try my best.

Using C# code in PowerShell Scripts

Hi guys, very short post today! Since PowerShell has access to .NET framework assemblies, you can use C# code either from source *.cs files or embed it directly in your script like I’ve done here.

Then you can call on your C# methods using the following from the prompt:

PS > .\csharp_in_powershell.ps1
PS > [CSharpPSTest.PSTest]::Get()
Get Success: Default Value
PS > [CSharpPSTest.PSTest]::Set("New Value")
Set Success: New Value

If you’re using a lot of functions in your script then writing them in C# can be a good way to simplify the logic as right now there’s much better tool support available for C# than PowerShell. You can do a lot more with your C# code than what I’ve shown here but hopefully it’ll be a good starting point for you!

That’s Life

Some people don’t blog because they can’t think of anything to write about.

I don’t blog because I have too bloody much to write about.

It might be time to start managing my time . . . .

How Safe Is Your Privacy Online?

So I was just reading Paul O’Flaherty’s blog and came across this. Well I headed over to http://whattheinternetknowsaboutyou.com/ to see how safe I was, and I got this (Click for Full Size)

Good no? I don’t even wear a tinfoil hat 🙂 So what about you, how safe are ya? The Internet at least now knows I use Linux Mint and Firefox, and in case you go pointing fingers, Transmission is downloading the latest Ubuntu Karmic alpha!

I got my college offers!

Well I knew from twitter leaks last night I’d gotten my coarse, but clicking accept on cao.ie is a whole different ballgame, and a relief to be perfectly honest. As of September I will be studying Software Development in Computing at GMIT Galway. I’m over the moon, and can’t wait to get started. Congratulations to all other Leaving Cert students, and to those of you in Galway, let’s do drinks sometime 😀

I See Radiohead Have a New Song

Recently the last remaining UK veteran of the 1st world war Harry Patch died at the age of 111.
I had heard a very emotional interview with him a few years ago on the Today program on Radio4.
The way he talked about war had a profound effect on me.
It became the inspiration for a song that we happened to record a few weeks before his death.
It was done live in an abbey. The strings were arranged by Jonny.
I very much hope the song does justice to his memory as the last survivor.

It would be very easy for our generation to forget the true horror of war, without the likes of Harry to remind us.
I hope we do not forget.

As Harry himself said
“Irrespective of the uniforms we wore, we were all victims”.

This morning the Today program played the song for the first time and now it is available to download from our website.

Please click here to download.

The proceeds of this song will go to the British Legion.

To peace and understanding.

Thom

Banging. Quite a beautiful song, and well worth a pound in my opinion.

Sick of Eircom Broadband Downtime? How to switch to OpenDNS

 

I know I’m not the only one who has really suffered this week with Eircom downtime (Apparently their DNS servers were hacked. The lady I asked about this on the phone convincingly said “Ah no, I don’t think they were hacked now, is that even possible?”) and just switched to OpenDNS, which was suggested by Komplett.ie’s blog (Thanks Komplett!). The process was relatively painless but if you’re not tech savvy, then the guide offered for Motorola routers likely puzzled you. Luckily Thomas is here to save the day and break down the process for you. I won’t provide screenshots as I don’t think they’re necessary as it’s quite quick, but if you hit a obstacle feel free to leave a comment and I’ll help you to the best of my ability.

1) First, open your router’s settings page by opening it’s address (http://192.168.1.254/ if you’re using the router Eircom provides for free)

2) Hit “Expert Mode” on the left sidebar

3) Confirm that you want to enter expert mode, then click “configure” and then “connection”

4) Scroll down to “Primary DNS Server” and change it to “208.67.222.222”. Change “Secondary DNS server” to “208.67.220.220”.

Hit “Save and Restart Connection” at the bottom of the page, wait a few moments, and complete registration on the OpenDNS site.

And that’s it! I hope this guide helped you with your switch, and I hope you’re happy with your much more secure connection, but don’t be afraid to let Eircom know how annoyed you are with this whole situation. I’ll be expecting some sort of compensation on my bill, or Eircom can expect one less customer next year.