Thursday, October 31, 2013

Putting the Fun in Functional Programming

 

Introduction

Like everyone who has been programming for some time, I have a set of tools and utility functions which I have developed over time, and reuse often.

My library of utilities has been available for some time both as open-sourced code and a free-to-use NuGet package.

The purpose of this series of posts is to talk about observations in my evolution as a functional programmer, and how the implementation and approaches I tend to take these days increasingly use functional concepts.

As you browse the code, you’ll come across more utilities than the ones discussed in this blog series. This series is going to try and focus on those utilities which demonstrate some facet of functional programming.

 

What is Functional Programming? Why do I care?

Functional Programming is a programming paradigm that treats programs as the evaluation of mathematical functions and expressions.

One can conceive of a kind of spectrum of programming abstraction:

  • Assembly Language which abstracts away parameterized opcodes
  • FORTRAN, COBOL and BASIC introduce control flow statements and subroutines
  • Pascal and C which allow for rich user-defined types, abstracting data and largely obviating the GOTO construct
  • Modula-2 and C++ which introduce concepts of objects maintaining state, encapsulating both data and related functions
  • Java and C# 1.0 which introduce concepts of managed memory and a virtual runtime environment, allowing for simpler creation of objects and popularizing Exceptions
  • Modern C# which introduces concepts of true type parameterization, lambdas and attributes. LINQ is a very powerful programming extension which is available here.
  • Scala introduces type classes and F# provides full type inference. These languages also use pattern-matching and recursion constructs to obviate some familiar imperative programming patterns.
  • LISP, a proto-functional language, is actually a multi-paradigm language, and as such, generally provides a great introduction to programming. LISP relies heavily on recursion, obviates some of the familiar programming constructs such as for loops and variables, introduces the concept of isomorphic code and data which makes it trivial to generate and immediately execute a function, and provides powerful meta-programming support.
  • Haskell, which emphasizes purity to the extent of introducing the concept of IO Monads to limit the impact of side-effects on a pure functional language.

Other authors have spoken cogently about the experience of a programmer skilled in a language somewhere in the spectrum (say a Java expert) looking up towards a more abstract language and not being able to comprehend why a construct or concept from that language would be useful or even necessary.

Therefore, it isn’t unreasonable to ask why we would need a functional programming perspective when we can do almost all we need with our daily programming language. Hopefully, as we go through this blog series, we’ll get a flavour of the kinds of things that functional programming makes easier, or possible!

 

What’s coming down the track?

 

Acknowledgements

My involvement with functional programming goes all the way back to college, where teachers like Dr. Takis Metaxis introduced me to symbolic computing with LISP.

I highly recommend reading Real-World Functional Programming: With Examples in F# and C# by Tomas Petricek and Jon Skeet.

If you get a chance to listen to the “Programming with Purity” talk by Joseph Albahari, do so – he has one of the most lucid explanations of Monads around!

I have stood on the shoulders of giants by reading their blogs: The relevant posts by Eric Lippert, Wes Dyer and Stephen Toub are especially enlightening. Mike Hadlow’s blog is also good reading.

For a truly mind-blowing experience, see the 13-part series on Functional Programming by the one and only Erik Meijer, who, of course, has the last word on monads and Category Theory!

Do stick around, and I’ll try to explain how I synthesize some of these concepts into tools that I use frequently.

Tuesday, October 29, 2013

It’s been a while…but a lot has happened!

Can’t believe it’s been a whole year since I blogged anything!

And what a year it’s been!

This year has turned out to be one of the more interesting ones in my career – lots of very cool stuff that’s been taking up all my time…now that it’s coming to a close, I’m going to write up some blog posts to talk about what has happened!

Daily work stuff

In my day job, I work for a software development company that builds accounting software which runs in the cloud. The flagship product – the one I work on – is a .NET rewrite of a very successful C++/ISAM desktop app. The rewrite began as a desktop application replacement so it was designed as such, but with the emergence of cloud technologies, we decided to make the app a hybrid and offer a rich client interface to a cloud service.

The basic premise was a good one – the architecture was well layered so we were able to stick the internet between the server and the client and get the thing to run. However, the devil in the details was performance – the desktop application, quite reasonably, expected to have a fast, fat connection to a local database, but that changed once we separated the UI from the server.

Over the last couple of years, we tried all the quick wins and got the app out, but performance was still sub-optimal and we needed to come up with some fundamental changes to break the problem.

At the start of this year, a couple of us was given the opportunity to go in with a sledgehammer and try to break the problem out. In six weeks we had a pattern for some drastic improvements and we’ve spent the better part of this year rolling that pattern out through the most performance sensitive areas.

Big win!

I’ll blog about some of the technical approaches we have taken to get these dramatic improvements, but it’s safe to say that they are not for the faint-hearted!

We actually rounded off the last year with 3 months of really creative work building the first cut of the MYOB API. Working with @H_Desai and building stuff with ASP.NET Web API was a pretty satisfying experience!

Open Source Mania

In my copious free time (ha ha) I decided to clean up and open source a whole bunch of stuff.

Amongst the weaponry that is now public are:

BrightSword SwissKnife

A  (mostly functional-programming based) set of utilities to help with everyday programming.

I’ve got a blog series on practical functional programming which centres around SwissKnife coming.

Warning: Contains Monads.

BrightSword Squid 

Rhino Mocks is a nice way to create anonymous types for interfaces but it is slow as all heck and a bit unwieldy, so I decided to write a fast library to generate DTOs with behaviours for general purpose use. MVVM patterns with Property Change Notifications work straight from the tin! And it’s 3 orders of magnitude faster than Rhino!

Contains lots of nasty nasty IL and Reflection.Emit!

BrightSword Pegasus

A reference library for CQRS and (Dynamic) Queue Centric Workflows based on Windows Azure. This thing is seriously cool and makes short work of writing extensible, scalable cloud applications.

Most of the stuff is pretty well tested – and I even set up my own automated build-pipeline to monitor source control, compile, test and upload to nuget. So all these libraries can be referenced and used through nuget.

Use the Nuget Packages. Fork the repos. Submit Pull requests.

The Cloud Architecture Talk

@MaheshKrishnan and I have been doing talks together for a few years now, and we spend a fair bit of our time idly gossiping about technology and trends. We observed that the best-practice software patterns for the cloud aren’t well articulated and well understood by the community at large, so we decided to build a talk around the most common patterns.

We built a application to demonstrate the patterns as well, and drew up some diagrams. Unlike most of our other talks, we demonstrate very little code in the talk itself, but the code is all open source.

We are both humbled and pleasantly surprised by its reception!

So far, we’ve given it at:

  • The DDD! Melbourne Conference
  • MYOB technical brown bag series
  • Melbourne Azure Meetup & VIC.NET
  • Singapore .NET Dev SIG
  • TechEd Australia 2013

And we have been invited to give it at NDC London!

Yes. That’s right! We’re speaking at NDC London!

ndc_london

TechEd Australia 2013

This year’s TechEd was pretty special!

Went to the Gold Coast with @simonraikallen, @couchcoder and @shaun_a_wilde.

20130905_174921

Hanging with @shaun_a_wilde and @couch_coder

IMG_9933

With @MaheshKrishnan at the Microsoft stand

20130906_164019

20130906_164100

@MaheshKrishnan and I gave a slightly modified version of the Cloud Architecture Patterns talk focusing on the implementation aspects. It was pretty well received, and we got a fair bit of good feedback from the audience. Lively discussion and lots of questions are always a good sign that people paid attention to the material and that it was relevant.

IMG_9957

IMG_9962

Got to meet @linqpad – would’ve loved to have an hour pairing with him on some stuff to do with monads, but perhaps another day.

20130904_211817

Met and hung out with the usual suspects…

3F0C9904

Hanging with @RockyH

IMG_0523

@drmcghee and @frankarr. Didn’t know at the time that this was the last TechEd Frank Arrigo was going to be at – so I’m glad we got this shot!

I won a 3D printer at the raffle, but wasn’t around to collect it so I lost it! I still get guys poking fun at me for this!

Our company had been selected as a case study for our use of Azure, and it was a pretty special privilege to represent them at the last, conference-wide Lock Note session.

It was made all the more special because the Microsoft CVP responsible for Azure – Scott Guthrie – was presenting the Lock Note, and I got a chance to get on stage with him and talk for five minutes about how we used Azure.

20130906_154952IMG_0017

IMG_0019

IMG_0021

So the reason I wasn’t around to claim my printer was that I was hanging out with @ScottGu, rehearsing for the Lock Note, and I got to spend about half an hour just hanging with the Gu, who is, for the record, a very friendly, down-to-earth guy.

IMG_0030

Pretty awesome, all told!