Functional Programming Aspects in Scala :-

Lavlesh Singh
Analytics Vidhya
Published in
3 min readOct 11, 2020

--

Functional programming (FP) is a way of writing computer programs as the evaluation of mathematical functions, which avoids changing the state or mutating data. The programs are constructed using pure functions. Functional programs are always declarative, where the programming is done with declarations and expressions instead of statements.

Photo by Dmitry Ratushny on Unsplash

Functional programming languages are categorized into two groups:
1. Pure function
2. Impure function

Pure Function?

A function that has no side effects is called a pure function. So, what are side effects? A function is said to be having side effects if it does any of the following other than just returning a result:
• Modifies an existing variable.
• Reads from a file or writes to a file.
• Modifies a data structure (e.g., array, list).
• Modifies an object (setting a field in an object).
The output of a pure function depends only on the input parameter passed to the function. The pure function will always give the same output for the same input arguments, irrespective of the number of times it is called. The impure function can give different output every time it is called and the output of the function is not dependent only on the input parameters.

i’m using REPL command for a Example of pure function

Here are some the typical examples of pure functions:
• Mathematical functions such as addition, subtraction, division, and
multiplication.
• String class methods like length, toUpper, and toLower.

These Scala String methods are also pure functions:
isEmpty
length
substring

These are some typical examples of impure functions:
• A function that generates a random number.
• Date methods like getDate() and getTime() as they return different
values based on the time they are called.

Below are befits Of using Pure Functions:
* They don’t cause any implicitly changes in input.
* They are easier to test.
* They can be debug easily.

Note :- The last statement of the function is always a return statement in Scala.Hence, it is not necessary to explicitly specify the return keyword.The semicolon is not needed to specify the end of a statement in Scala. By default,the newline character (\n) is considered the end of a statement. However, asemicolon is needed if multiple statements are to be written in a single line.

Impure Functions in Scala?

All the functions that are not pure are impure. This makes the definition of an impure function as “A function that is dependent on i/o or other values out of function’s scope along with parameter values.”

Example: Constant multiplier :-

object MyObject {
val constant = 12.4;
def constantMul(value:Int): Double = {
val result = value*constant;
return (result);
}

def main(args: Array[String]) {
println("The result of constant multiplier is: " + constantMul(34))
}
}

Output:

The result of constant multiplier is: 421.6

In the above code, we have created a constantMul() function which accepts a parameter value that is multiplied by a value defined in the object i.e. dependent on a value outside the scope of the function.

Thanks for reading my Post :)

For understanding Basics of Scala language you can refer my previous post

--

--