There is new paradigm shift in the world of computer programming known as Functional Programming (FP). It is a paradigm of choice to unlock the power of multi core processors.
With the boom of parallel processing all major vendors of processors have decided unanimously that they shall be putting more and more cores in a die and it is up to software development to architect their solutions to make the optimize utilization of these cores . So ball lies in our court to take the advantage of parallel processing powers of their chips. Functional Programming (FP) has the potential to reduce program lines count by 30% to 60%, reduce bugs and increase robustness, and move us in the direction of taking advantage of multiple core CPUs.
FP is derived from the declarative style of branch language tree where there is no flow order defined. This is complete contrast of any object oriented programming language where the program flow order is from top to bottom.
The compelling features of FP languages are that all the instructions are treated as elevation of the function and all programs avoid mutable data. So it means a variable in FP is similar to a constant whose value cannot get changed during the execution of the program. This result into zero side affects as multiple threads will store the same value. This make FP has the obvious choice for multiple cores. Well after reading this doesn’t get paranoid that how can I code if I am not allowed to change the state of my variable? The answer lies in Lambda Calculus. All FP languages stored the states in function parameters that are put on the stack. In any FP language everything is a function. The bottom line is that entire program is one function and this function calls the other functions if existed to get the job done.
Functional programming has been around since the time of LISP since 1958.
With the release of .NET framework 3.5, new coding style is available. Though some features of FP was available in 2.0 version but the resultant code was ugly and not human readable. Now with lambda expressions and Compiler Extension methods lot has been changed and result much cleaner code.
Functional ideas are arising in C# as well as in other main-stream languages and functional languages are becoming an alternative for real-world projects. Also, Microsoft recently introduced a new language called F#, which has a strong background in traditional functional languages, but as a .NET language also benefits from the rich .NET and Visual Studio ecosystem. Thus it will be the first hybrid programming language from Microsoft stable which will support Object Oriented as well as Functional programming.
All you Java guys out there , don’t get disheartened . Scala is the FP language for you. It compiles into JVM byte codes which enables you to use Java Run-time libraries. There is a plug in also available for Eclipse.
In order to get the better understanding of the advantages that FP has to offer. I will demonstate a simple loop code in C# in non functional as well as functional style.
So let us get started with non functional style (State programming)
for (Int32 i = 0; i < 10; i++)
The above code is no rocket science , which demonstates a simple iteration code.
The same thing can be achieved in the functional programming style. We need to define the compiler extension methods for the same, which can be coded in module.
public static class FunctionalExtensions
public static void ForEach<T>(IEnumerable<T> items, Action<T> PerfomAction)
foreach (T item in items)
The above code defines a static method ForEach. A generic approach has been used here which are true greats in .Net Framework.
The utilization of this function can be made by the following code
FunctionalExtensions.ForEach(Enumerable.Range(0, 10), Display);
public void Display(Int32 i)
Now if you compare two styles it is very much evident that the functional approach is far better than the non functional approach.
We get the following advantage for it
1) Code Reuse
2) Unit Testing
3) Multi threading and asynchronous functionality
Knowledge of FP is now become the must have skill.Developers are taking a step back thinking it’s a steep learning curve for them.
Believe me guys this stuff is really easy. It is also fun to code. You don’t have to scan your academics to get started with it. So get the most out of your hardware by kick starting today in Functional Programming language of VB9.0 or C# 3.0 and extend your skill sets and position your career to new heights.
Till then Happy Coding.