Click here to Skip to main content
15,885,546 members
Articles / Functional

Functional Programming

Rate me:
Please Sign up or sign in to vote.
3.76/5 (10 votes)
13 May 2019CPOL2 min read 6.7K   6   2
Functional Programming Advantages

Introduction

The first step in software engineering is to have a good knowledge of programming languages, this step leads us to select a proper language according to the specific scenario. There are two main paradigms - Imperative and declarative. Imperative languages use a sequence of statements in order to accomplish a purpose.

What is Functional Programming?

There are explicit initialization and value assignments, while declarative languages use only expressions without any detailed control flow. Functional programming is a subset of declarative programming and Object Oriented Programming is a subset of imperative languages. C# and Java are OOP languages but F# and Scala are Functional languages. Although it is possible to use OOP concepts such as classes, interfaces, and inheritance in Functional languages and vice versa, for example using lambda expressions concept in C# which is a Functional language.

Python
 //Imperative
        string[] stringList = new string[] { "Mahsa", "Kashi", "Hassankashi" };
        List oopList = new List();
        foreach (string str in stringList)
        {
            if (str.Length == 5)
            {
                oopList.Add(str);
            }
        }
        
//Declarative - Functional
        List functionalList = stringList.Where(s => s.Length == 5 ).ToList();

Why Functional Programming?

The most important reason why functional programming is on the cutting edge of technology is because of having efficient and reliable behavior when working with multiple processors or in better words, it has the best performance while working with parallel programming.

Avoiding Stack Overflow by Tail Call Optimization

Another reason is that loops and iterations in functional languages will be done via recursion with TCO (Tail Call Optimization). TCO is an approach to call the function without separated stack frames work and it avoids stack overflow. For example in the below code, Factorial(4) computation needs 4 separate stack frame while the next one in functional languages needs just one stack frame.

It is not TCO:

Python
def factorial(n):
      if n == 0:
      return 1
      return n * factorial(n-1)

This is TCO:

Python
def factorial(n):
     return fact(n, 1)

 def fact(n, number):
     if n == 0:
     return number
     return fact(n-1, number*n)

Avoiding race condition and deadlock with the aid of immutable objects as thread safe.

What Is the Race Condition?

In multi-threading concept, when two or more threads try to catch a shared resource or object at the same time.

What is Deadlock?

In multi-threading concept, when process 1 holds and locks resource 2 and waits for resource 1, while exactly at this time, process 2 holds and locks resource 1 and waits for resource 2.

What is Thread-safety?

In multi-threading concept, thread safe ensures that each thread can perform its execution properly in simultaneous ways without unintended interactions.

What is Immutable?

Immutable object's state or value cannot be changed after creation. a is mutable, because its value will be changed:

  • a = 1
  • b = 2
  • a = a + b

In creation time a should be defined as read only and immutable to prevent changing its value.

Conclusion

Because immutable is thread safe in functional programming; therefore it is useful for infinitive for or foreach loop - special in mathematic functions - and prevent race condition and Deadlock.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Doctorandin Technische Universität Berlin
Iran (Islamic Republic of) Iran (Islamic Republic of)
I have been working with different technologies and data more than 10 years.
I`d like to challenge with complex problem, then make it easy for using everyone. This is the best joy.

ICT Master in Norway 2013
Doctorandin at Technische Universität Berlin in Data Scientist ( currently )
-------------------------------------------------------------
Diamond is nothing except the pieces of the coal which have continued their activities finally they have become Diamond.

http://www.repocomp.com/

Comments and Discussions

 
GeneralMy vote of 5 Pin
Igor Ladnik5-Jun-19 19:42
professionalIgor Ladnik5-Jun-19 19:42 
QuestionTail Call Optimization Pin
George Swan13-May-19 20:53
mveGeorge Swan13-May-19 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.