..


Sponsored Links

Introduction to functional programming in C #

Article written by Vincenzo Gaglio
Page 2 of 2

At first you may be wondering what is the advantage of this second type of approach than the first. In fact there are various advantages of using these techniques:

Code reuse - It 'just defined functions can be reused in different parts of our code. This benefit is not apparent with our simple example, but imagine if you have functions that perform complex operations, the fact of being able to call without having to rewrite the code contained in them in several places of our programs is a major advantage and also facilitates maintenance because of our applications when you need to change something just do it and not only in the function at several points.

(The article continues below ...)

Ease of debugging and testing - Just go to any function provided the appropriate parameters to verify its operation, with no dependencies on external factors.

Competition - It 'can be used multiple times in parallel with the code of our functions.

The most obvious disadvantage of the functional approach is the impact that the number of items in the 'heap (ie in memory) when you find yourself having to instantiate different objects (one for each function call) instead of having a single object shared. This is something to consider if we are to implement applications that must manage multiple concurrent calls.

Now let's see another example, which is a routine that reads a text file and count the number of occurrences of each word. The non-functional version of the routine is as follows






 private static void EsempioNonFunzionale (string [] args)







 {



    



 using (StreamReader rdr = File.OpenText (args [0]))



    



 {



        



 String content = rdr.ReadToEnd ();



        



 String [] words = contenuto.Split ('');





        



 for (Int32 i = 0; i <parole.Length; i + +)



            



 words [i] = words [i]. Trim ();





        



 Dictionary d = new Dictionary <String, Int32> <string, int> ();





        



 foreach (string word in words)



            



 if (d.ContainsKey (word))



                



 d [word] + +;



            



 d.Add else (word, 1);





        



 foreach (KeyValuePair <String, Int32> KVP in d)



            



 Console.WriteLine (String.Format ("{0} number {1}",

 

      



 kvp.Key, kvp.Value.ToString ()));



    



 }







 }



The functional version (which also use lambda expression) is as follows






 private static void EsempioFunzionale (string [] args)







 {

 

   



 File.OpenText (args [0]). Use (stream =>



       



 {



            



 stream



                



 . ReadToEnd ()



                



 . Split ('')



                



 . Convert (str => str.Trim ())



                



 . GetCounts ((x, y) => x == y)



                



 . ForEach (KVP => String.Format ("{0} number {1}",

 

        



 kvp.Key, kvp.Value.ToString ()));



        



 });







 }



As you can see from the non-functional version of the functional we went to about half of lines of code. Summing up, therefore, in functional programming, functions are used as objects, which are addressed in a program so closely analogous to the variables.

The purpose of this article is to introduce this interesting technique that can be used to write the code of our programs more efficiently and have the advantages mentioned above. Probably it will be followed-depth on the subject, since it is quite large and has several themes worth investigating.

In the same category ...
E-Learning
ASP and Access Course ASP and Access Course
Managing a MS Access database with ASP. At only 29 €.
Java Course Java Course
OOP programming in Java SUN. Discount -10% until 06/01/2012.
OpenOffice Course OpenOffice Course
The open-source software for the management of office work. At only 25 €.
Sponsored Links