Change gears to Functional Programming

INTRODUCTION

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.

.NET INITIATIVE

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.

CODE COMPARISON

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++)
Console.WriteLine(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)
PerfomAction(item);
}
}
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)
{
Console.WriteLine(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

CONCLUSION

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.

Advertisements

6 thoughts on “Change gears to Functional Programming

  1. Yes indeed it is best methodology to use the full power of hardware. It is going to change the programming world. Mark my words.

  2. Pingback: LINQ —> The new mantra of querying data « Getting Deep into .NET

  3. I do not know whether it’s just me or if everyone else encountering problems with your blog. It seems like some of the text on your posts are running off the screen. Can someone else please provide feedback and let me know if this is happening to them too? This may be a issue with my browser because I’ve had
    this happen before. Thanks

    • Hi
      I have heard about the issue only from you. I am using LiveWriter for authoring my blog posts.
      I have also checked the layout with almost all modern browsers.

      Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s