Introduction to Scala Programming Language

Scala

Scala means Scalable Language.
Scala is object oriented programming language and also functional programming language.
Scala official website: https://www.scala-lang.org
Wiki Page: https://en.wikipedia.org/wiki/Scala_(programming_language)





Object Oriented Programming(OOP)

  • Object oriented programming is a programming model which is based on objects 
  • Using this, we can easily visualize real life scenarios 
  •  Program has two basic elements, data and operations that you want to work on the data 
  • In OOP, both data and operations in one location 
  • Each box you see here is represents class, you can create multiple instances of class called object. Object relates to what you have in the real world


Functional Programming

  • In pure functional programming, there is no concepts of classes and objects, which means data and operations that you want to work on the data is not in the centralised location 
  • In functional programming, it treats data and operations are two different things, hence they kept separate 
  • The idea in functional programming is to break up big job into small/tiny manageable functions, each function is very specific work to do



Object Oriented Programming(OOP) vs Functional Programming



Different Types of Functions in Scala

Anonymous Functions

Create a list with numbers from 1 to 10
val numbersList = List.range(1, 10)
We can pass an anonymous function to the List’s filter method to create a new List that will have only even numbers:
val evens = numbersList.filter((i: Int) => i % 2 == 0)
The following code is called function literal (also known as an anonymous function)
(i: Int) => i % 2 == 0



Higher Order Functions

A higher-order function is special type of function which takes other functions as a input parameter or returns a function as a output.

The map function is a classic example of a higher order function.
val numbersList = List(1, 2, 3, 4, 5)
Now go and define a function that does squares each value that is passes to it.
def squareValue = (x: Int) => x * x
val squaredList = numbersList.map(x => squareValue(x))
println(squaredList)

Partial Functions

Partial functions are functions, which are not defined for all possible values of input parameter data type.

Happy Learning !!!

Post a Comment

0 Comments