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.
//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:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
This is TCO:
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:
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.