Functional and Concurrent Programming
INTRODUCTION:
TOPICS COVERED-FUNCTIONAL PROGRAMMING IN SCALA, TAIL-RECURSIVE FUNCTIONS USING SCALA, OBJECT-ORIENTED LANGUAGE(OOL), CONCURRENT PROGRAMMING.
FUNCTIONAL PROGRAMMING:
Functional programming is writing pure functions. Functional programming is based on mathematical functions. Some of the popular functional programming languages include Lisp, Python, Erlang, Haskell, Clojure, etc. ...It helps us to solve problems effectively in a simpler way. It improves modularity. It allows us to implement lambda calculus in our program to solve complex problems. Functional programming is a programming paradigm where programs are constructed by applying and composing functions.
Why is Scala a functional programming language?
Scala is a functional programming language as it supports functional programming. ... A functional language, Scala does not allow any mutable state as it creates a problem of synchronization on its shared access. Scala supports this model with its Actors library but allows for both mutable as well as immutable variables.
TAIL-RECURSIVE FUNCTIONS IN SCALA:
Recursion is a method that breaks the problem into smaller subproblems and calls itself for each of the problems. That is, it simply means function calling itself. The tail-recursive functions are better than non-tail-recursive functions because tail-recursion can be optimized by the compiler. A recursive function is said to be tail-recursive if the recursive call is the last thing done by the function. There is no need to keep a record of the previous state.
For the tail recursion function a package import scala.annotation.tailrec will be used in the program.
Syntax:
@tailrec
def FuntionName(Parameter1, Parameter2, ...): type = …
Example for tail-recursive function:Find factorial of a number
import scala. annotation. tailrec
object MyObject
{
def factorial(n: Int): Int =
{
@tailrec def factorialAcc(acc: Int, n: Int): Int =
{
if (n <= 1)
acc
else
factorialAcc(n * acc, n - 1)
}
factorialAcc(1, n)
}
def main(args:Array[String])
{
println(factorial(5))
}
}
Output:
120
OBJECT-ORIENTED LANGUAGE(OOL) IN SCALA:
Scala is a hybrid between functional and object-oriented programming, and it smoothly integrates the features of object-oriented and functional languages.
OOL is a high-level computer programming language that implements objects and their associated procedures within the programming context to create software programs.
Example code snippet to explain OOL in SCALA:
class Student(id:Int, name:String){
def getDetails(){
println(id+" "+name);
}
}
object MainObject{
def main(args:Array[String]){
var s = new Student(190030320,"Mahi");
var s1 = new Student(190031367,"Priya");
s.getDetails();
s1.getDetails();
}
}
Output:
190030320 Mahi
190031367 Priya
CONCURRENT PROGRAMMING IN SCALA:
What is concurrent programming?
Concurrency is when more than one task can start and complete in overlapping time periods. It doesn’t matter whether they’re running at the same instant. You can write concurrent programs on a single CPU (single execution core) machine where only one task can execute at a given point in time. Typically multiple tasks are executed in a time-slice manner, where a scheduler (such as the JVM) will guarantee each process a regular “slice” of operating time. This gives the illusion of parallelism to the users. And the common de facto standard way to implement a multitasking application is to use threads.
Scala concurrency is built on top of the Java concurrency model. On Sun JVMs, with an IO-heavy workload, we can run tens of thousands of threads on a single machine. A Thread takes a Runnable. You have to call start on a Thread in order for it to run the Runnable.
CONCLUSION:
##Functional programming is all about static truths such as in Math, is easier to reason about, is tractable, and in a lot of places solves our problems better than imperative solutions.
##We claim that concurrency is crucial for scalability, which in turn is inherently critical for large-scale architectures. The growing prevalence of web-based applications thus requires both scalable architectures and appropriate concepts for concurrent programming. Although web-based applications have always faced inherent parallelism, the concurrency implications for architectures and implementations are gradually changing.