C# 3.0 Higher Order Functions

I’ve been reading a lot about F# and functional programming, and it has indeed been pretty head-turning but it’s really powerful if you know how to use it. Type Inference in F# compiler is spectacular compared to the C# one, but I shall not digress. This post is about showing off the functional aspects of C# 3.0, namely Higher Order Functions. In short, higher order functions are functions that can take in one or more functions as input parameters, as well as return a function as an output. In C# 3.0, delegates have evolved a lot and with the introduction of the System.LINQ namespace, plus a lot of functional features have been incorporated into the language, and we’ll see how to make full use of them.

In the System.LINQ namespace, you will find a large bunch of Extension Methods for IEnumerable<T> class. Most of them are higher order functions, meaning that they take in a delegate (or pointer to a function) as an input parameter. Some of these delegate types taken as inputs are Func<T, TResult>, Action<T> and Predicate<T>. What this means is that you can pass in an Anonymous Function (Lambda Expressions and Anonymous Methods) to enable different kind of behaviors. If you’re familiar with the Strategy Pattern, programmers use this pattern in order to “plug in” different behaviors flexibly by writing a family of algorithms encapsulated within derived strategy classes. With higher order functions, I’m inclined to believe that we now have something more straightforward and powerful at our disposal.

Read the rest of this entry »


Moving to a functional state of mind with F#

In my previous post, I talked about my uneventful journey of learning a functional language and how it was revived again by discovering this great book called Functional Programming for the real world. What’s great about this book is that the authors prepare you mentally for the transition to a functional state of mind. If you are not interested in functional programming, understanding the key aspects of it is still beneficial because it opens up a different approach to solving problems, which is never a bad thing. For example, functional programmers aim to write elegant and succinct code, as well as aiming to be natural to read by making use of it’s declarative nature. Once you learn this skill, you can apply this even to static language, as we will soon see.

Read the rest of this entry »

Is there a functional programmer inside you?

I only just realized I have had a crush on Functional Programming for ages. When I first learned and used Generics and Anonymous Methods in .NET 2.0, I instantly fell in love. Next came C# 3.0, LINQ and XAML, and I was instantly drawn to it’s declarative nature and style. Having done programming for a relatively short time, I delved mostly in Imperative Programming in statically typed languages like C#, VB.NET and C++. C# 3.0 had a facelift by getting some aspects of functional programming (like lambda expressions) and that made it more powerful, fun and simpler to write/understand. Eventually I decided to venture into the world of Functional Programming, namely F#.

Read the rest of this entry »