SlideShare a Scribd company logo
What is Scala
• A General purpose programming language.
• Supports both object oriented and functional programming styles.
• Martin Odersky developed Scala in 2001.
What is Scala
• Fully supports OOPS
• Everything is an object in Scala.
• Unlike java Scala does not have primitives.
• Functions themselves are objects in Scala.
• Functions are first class citizens in FP. Means they can be assigned to variables, passed to other functions,
etc.. Just like other values.
• Scala is a statically typed.
• Supports static class members through singleton object concept.
• Improved support for OOP through Traits.
Scala
Scala
Scala REPL
• REPL – Read – Evaluate – Print – Loop
• Easiest way to get started with Scala. Act as an interactive shell interpreter.
• It converts all typed code to bytecode and executed.
• Invoked by typing Scala.
Variable Types
• In Scala you can decide if your variables are mutable or immutable.
• Val
• Similar to Java Final Variables.
• Once initialized Val’s cant be reassigned.
• By default it assigns to val.
• Var
• Similar to non final variable in java.
• Scala infers the type.
• val a : Int = 0 is equal to val a = 0
Automatically initialize it to session variable res4
Automatically assigned as Integer
res is default naming convention for Scala vals.
0/1/2 followed by res is auto increment numbering
Automatically assigned as String.
Even though we never declared variable type, scala inferred it.
If you assigned a variable as val. It’s a constant and you cannot re assign it.
This will work because we are redefining it.
Instead of Val (read only) we are using var to assign
a variable, and hence it allows us to re assign it to
any other value.
You cannot re assign a variable to value of other
data type.
Once a type is assigned to a variable, it remains
same for entire scope.
This will not work, since val is read only (Constant)
Assigning Block Expression
• In an expression the last statement will be returned.
Lazy Values
• You can define a value as lazy in scala.
• Lazy value initialization is deferred till its accessed for first time.
If you want to read a file and if the file is not existing or present ,
you will get FileNotFoundException.
But if you initialize the value as lazy, you wont get this error,
because it will delay the initialization till it access the file.
mkString is similar to toString in java.
Control Structures in Scala
If Else Condition
If Else Condition where 2 different data types are returned in a
single if- else condition.
The return type will be Any class.
For each loops iterate without an iterator.
x.foreach is an implicit iteration where you don’t have to
create any iterator.
i <- 0 implies int i = 0
It sums every index.
Nested For Loop
Nested For Loop in Java
• for( int i = 0 ; i < 3; i++)
• for( int j =0 ; j<3 ; j++)
• println(i*j)
• Nested for loop in Scala
• for( i<- 1 to 3 ; j <- i to 3)
• Print(i*j)
Nested For Loop (Contd.)
Functions
Methoddefinition
Nameofmethod
Parameterofmethod
Datatypeofparameter
Returntypeofmethod
Bodyofmethod
Function Declaration
Return type Double is optional. Because return type is
dynamically inferred.
If there is only one line in body, curly braces can be
removed.
Return type is mandatory if the function is a recursive
function.
Named arguments.
Passing only one
argument. Cos 2nd and
3rd are has default
values.
Arguments in Functions
Procedures
• Procedures don’t have any return type. Its similar to functions.
• If scala has a function without a preeding “=“ symbol then the return of the function is Unit.
• Procedures do not return any value in scala.
Collections
Declare an array
Fetch value based on index
Assign value on specific index
Iterate over an array.
Array
Even though n is val. The
values can be reassigned.
Mutable version of array. We need to
explicitly import.
Array Buffer
Elements can be added and size can be
increase dynamically, unlike array.
Similar to Java ArrayList
Array Buffer and Array Functions
Array Buffer and Array Functions
By default creates
immutable map
Cannot edit an
immutable map.
Even if you create
using var. it remains
immutable.
Map
Map Functions
Mutable Map
Tuples
• Tuple has more than two values of potential different types.
• In tuples the offset start with 1 and Not from 0.
• Tuples are typically used for functions which return more than one value.
List and their Functions
OOPS in Scala
Creation object without
parenthesis
Creation object
Classes
Properties with Getter and Setter
• Scala generate a class for JVM with a private size variable and public getter and setter methods.
• If the field is declared as private, the getter and setter would be private.
• The getter and setter methods in previous case would be:
• size and size_ =
Scala generates getter and
setter methods for
member variables.
Cannot access
private variable.
And Scala does
not generate
getter and setter
for private
variable.
Auxiliary constructor should call default/primary constructor in first line. Or the
previous auxiliary constructor. In this case its default constructor this()
Auxiliary constructor if not primary constructor can call previous auxiliary
constructor in first line. this(age)
Auxiliary Constructor
Auxiliary constructor in scala is called this()
Primary Constructor
1. Every class in scala has a primary constructor.
2. Parameters for primary constructor are placed
immediately after the class name.
Singletons
• Scala doesn't have the concept of static methods or fields.
• It is supported by object construct.
• An object defines the single instance of class.
Companion Objects
• In programming languages we have both instance methods and static methods in same class.
• In Scala, it is achieved using companion objects.
• The class and its companion objects need to be in same source file.
• The companion object of the class is accessible, but NOT in scope.
• The class and its companion objects can access each other’s private features.
Static in Java
Singletons and Companion Objects
Static example in
previous slide, can be
done in scala using
companion objects.
Compare two objects.
The below comparison can be made simple using Case notion in Scala Classes.
Case Class
• Case class don’t need a constructor for them
• For case class the scala compiler:-
• Creates the class and its companion object.
• Makes all parameters as val, so we can have only accessors.
Extending a Class
• Just like java, classes can be extended using extends keyword.
• Just like java, new methods and fields can be introduced or superclass methods or fields could be overridden in subclasses.
• A class can be declared as final and avoid being overridden.
•
Extending a Final Class
• Final class can not be overridden.
Overriding Methods
• Override modifier must be used to override an abstract methods.
• Invoking superclass method is same as in java, by super keyword.
Traits as Interface
• Scala traits works exactly like java interface.
• Methods need not be declared as abstract.
• extend keyword is used to implement a trait in scala.
• with keyword is used to extend more than one interface.
Traits as Interface
Traits as Interface
• Traits can have concrete and abstract methods.
• If traits have a method with body, then the class which extends should use override keyword
while extending it.
• Traits cannot be instantiated.
• Abstract method need a definition when a trait is extended.
• Multiple traits can be overridden using with keyword.
• A namespace collision is taken care by overriding the definition of the method with the last
available definition.
App trait
• Since everything in scala is an object, even main method lives in a object.
• There are two ways to create a launching point for your application: define an object that extends
the App trait, or define an object with a properly defined main method.
When using this approach, any command-line arguments to your
application are implicitly available through an args object, which is
inherited from the App trait. The args object is an instance of
Array[String], just as if you had declared a main method yourself. The
following code demonstrates how to use the args object:
The second approach to launching an application is to manually
implement a main method with the correct signature in an object, in a
manner similar to Java:
Functional Programming in
Scala
Functional Programming
• Functional programming is mainly driven by
• Functions are first class citizens. They are treated just as any other type say string, integer etc. so functions can be
used as arguments , could be defined in other functions.
Higher Order Functions
Functions are first class citizens.
They can be passed as a parameter and returned as a result.
Functions that take other functions as parameter or that return functions as result are called
Higher order functions.
Examples
1. Decorator class has a primary constructor and takes 2
arguments as a parameter right and left of type String.
2. Decorator class has a function layout within.
3. layout accepts a parameter x which is of type A ( user
defined type similar to T in java). [A] is type safety.
4. And layout returns a string which sandwiches argument
x with left and right arguments.
1. Create a method with name apply which receives 2
parameter. Function f of type int , and v of type int.
2. F is an function which receives int as an argument and
returns string as an value.
3. f : Int => String denotes function as a parameter.
4. Body of the function has f(v) which means the second
argument v is passed as an argument to the function
which was passed as an first argument.
1. Create an object to decorator class.
1. Call apply method and pass decorator.layout function as
an argument and a number 7 as a value.
Examples 2
This method has eliminated the need of sumInts,
sumCubes and sumSquares function.
Anonymous function has eliminated the need of other
remaining functions sum, squares and cube as well.
5 functions are created. Lets see how
we can minimize with help of higher
order functions.

More Related Content

What's hot

Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
manaswinimysore
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java Library
Knoldus Inc.
 
Scala basic
Scala basicScala basic
Scala basic
Nguyen Tuan
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objects
İbrahim Kürce
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Java Tutorial Lab 1
Java Tutorial Lab 1Java Tutorial Lab 1
Java Tutorial Lab 1
Berk Soysal
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
abdullah al mahamud rosi
 
Ap Power Point Chpt8
Ap Power Point Chpt8Ap Power Point Chpt8
Ap Power Point Chpt8dplunkett
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
Ahmad sohail Kakar
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
Arpana Awasthi
 
Variable
VariableVariable
Java static keyword
Java static keywordJava static keyword
Java static keyword
Ahmed Shawky El-faky
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
GeekNightHyderabad
 

What's hot (18)

Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java Library
 
Scala basic
Scala basicScala basic
Scala basic
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objects
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Java Tutorial Lab 1
Java Tutorial Lab 1Java Tutorial Lab 1
Java Tutorial Lab 1
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
 
Ap Power Point Chpt8
Ap Power Point Chpt8Ap Power Point Chpt8
Ap Power Point Chpt8
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
Variable
VariableVariable
Variable
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 

Similar to Introduction to Scala

Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
chambeda
 
Java
JavaJava
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
ManikandanRamanujam
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
Khaled Anaqwa
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
VasanthiMuniasamy2
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
Srizan Pokrel
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
C# interview
C# interviewC# interview
C# interview
Thomson Reuters
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
Shipra Swati
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
Knolx Session: Introducing Extractors in Scala
Knolx Session: Introducing Extractors in ScalaKnolx Session: Introducing Extractors in Scala
Knolx Session: Introducing Extractors in Scala
Ayush Mishra
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 

Similar to Introduction to Scala (20)

Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
 
Java
JavaJava
Java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
 
Javasession6
Javasession6Javasession6
Javasession6
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
C# interview
C# interviewC# interview
C# interview
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Knolx Session: Introducing Extractors in Scala
Knolx Session: Introducing Extractors in ScalaKnolx Session: Introducing Extractors in Scala
Knolx Session: Introducing Extractors in Scala
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 

More from Viyaan Jhiingade

Rate limiting
Rate limitingRate limiting
Rate limiting
Viyaan Jhiingade
 
No sql
No sqlNo sql
Rest Webservice
Rest WebserviceRest Webservice
Rest Webservice
Viyaan Jhiingade
 
Storm
StormStorm
Git commands
Git commandsGit commands
Git commands
Viyaan Jhiingade
 
Jenkins CI
Jenkins CIJenkins CI
Jenkins CI
Viyaan Jhiingade
 
Kafka RealTime Streaming
Kafka RealTime StreamingKafka RealTime Streaming
Kafka RealTime Streaming
Viyaan Jhiingade
 

More from Viyaan Jhiingade (7)

Rate limiting
Rate limitingRate limiting
Rate limiting
 
No sql
No sqlNo sql
No sql
 
Rest Webservice
Rest WebserviceRest Webservice
Rest Webservice
 
Storm
StormStorm
Storm
 
Git commands
Git commandsGit commands
Git commands
 
Jenkins CI
Jenkins CIJenkins CI
Jenkins CI
 
Kafka RealTime Streaming
Kafka RealTime StreamingKafka RealTime Streaming
Kafka RealTime Streaming
 

Recently uploaded

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

Introduction to Scala

  • 1.
  • 2. What is Scala • A General purpose programming language. • Supports both object oriented and functional programming styles. • Martin Odersky developed Scala in 2001.
  • 3. What is Scala • Fully supports OOPS • Everything is an object in Scala. • Unlike java Scala does not have primitives. • Functions themselves are objects in Scala. • Functions are first class citizens in FP. Means they can be assigned to variables, passed to other functions, etc.. Just like other values. • Scala is a statically typed. • Supports static class members through singleton object concept. • Improved support for OOP through Traits.
  • 6. Scala REPL • REPL – Read – Evaluate – Print – Loop • Easiest way to get started with Scala. Act as an interactive shell interpreter. • It converts all typed code to bytecode and executed. • Invoked by typing Scala.
  • 7. Variable Types • In Scala you can decide if your variables are mutable or immutable. • Val • Similar to Java Final Variables. • Once initialized Val’s cant be reassigned. • By default it assigns to val. • Var • Similar to non final variable in java. • Scala infers the type. • val a : Int = 0 is equal to val a = 0
  • 8. Automatically initialize it to session variable res4 Automatically assigned as Integer res is default naming convention for Scala vals. 0/1/2 followed by res is auto increment numbering Automatically assigned as String. Even though we never declared variable type, scala inferred it. If you assigned a variable as val. It’s a constant and you cannot re assign it.
  • 9. This will work because we are redefining it. Instead of Val (read only) we are using var to assign a variable, and hence it allows us to re assign it to any other value. You cannot re assign a variable to value of other data type. Once a type is assigned to a variable, it remains same for entire scope. This will not work, since val is read only (Constant)
  • 10. Assigning Block Expression • In an expression the last statement will be returned.
  • 11. Lazy Values • You can define a value as lazy in scala. • Lazy value initialization is deferred till its accessed for first time. If you want to read a file and if the file is not existing or present , you will get FileNotFoundException. But if you initialize the value as lazy, you wont get this error, because it will delay the initialization till it access the file. mkString is similar to toString in java.
  • 12. Control Structures in Scala If Else Condition If Else Condition where 2 different data types are returned in a single if- else condition. The return type will be Any class. For each loops iterate without an iterator.
  • 13. x.foreach is an implicit iteration where you don’t have to create any iterator. i <- 0 implies int i = 0 It sums every index.
  • 14. Nested For Loop Nested For Loop in Java • for( int i = 0 ; i < 3; i++) • for( int j =0 ; j<3 ; j++) • println(i*j) • Nested for loop in Scala • for( i<- 1 to 3 ; j <- i to 3) • Print(i*j)
  • 15. Nested For Loop (Contd.)
  • 18. Return type Double is optional. Because return type is dynamically inferred. If there is only one line in body, curly braces can be removed. Return type is mandatory if the function is a recursive function.
  • 19. Named arguments. Passing only one argument. Cos 2nd and 3rd are has default values. Arguments in Functions
  • 20. Procedures • Procedures don’t have any return type. Its similar to functions. • If scala has a function without a preeding “=“ symbol then the return of the function is Unit. • Procedures do not return any value in scala.
  • 22. Declare an array Fetch value based on index Assign value on specific index Iterate over an array. Array Even though n is val. The values can be reassigned.
  • 23. Mutable version of array. We need to explicitly import. Array Buffer Elements can be added and size can be increase dynamically, unlike array. Similar to Java ArrayList
  • 24. Array Buffer and Array Functions
  • 25. Array Buffer and Array Functions
  • 26. By default creates immutable map Cannot edit an immutable map. Even if you create using var. it remains immutable. Map
  • 29. Tuples • Tuple has more than two values of potential different types. • In tuples the offset start with 1 and Not from 0. • Tuples are typically used for functions which return more than one value.
  • 30. List and their Functions
  • 33. Properties with Getter and Setter • Scala generate a class for JVM with a private size variable and public getter and setter methods. • If the field is declared as private, the getter and setter would be private. • The getter and setter methods in previous case would be: • size and size_ =
  • 34. Scala generates getter and setter methods for member variables. Cannot access private variable. And Scala does not generate getter and setter for private variable.
  • 35. Auxiliary constructor should call default/primary constructor in first line. Or the previous auxiliary constructor. In this case its default constructor this() Auxiliary constructor if not primary constructor can call previous auxiliary constructor in first line. this(age) Auxiliary Constructor Auxiliary constructor in scala is called this()
  • 36. Primary Constructor 1. Every class in scala has a primary constructor. 2. Parameters for primary constructor are placed immediately after the class name.
  • 37. Singletons • Scala doesn't have the concept of static methods or fields. • It is supported by object construct. • An object defines the single instance of class.
  • 38. Companion Objects • In programming languages we have both instance methods and static methods in same class. • In Scala, it is achieved using companion objects. • The class and its companion objects need to be in same source file. • The companion object of the class is accessible, but NOT in scope. • The class and its companion objects can access each other’s private features.
  • 40. Singletons and Companion Objects Static example in previous slide, can be done in scala using companion objects.
  • 41. Compare two objects. The below comparison can be made simple using Case notion in Scala Classes.
  • 42. Case Class • Case class don’t need a constructor for them • For case class the scala compiler:- • Creates the class and its companion object. • Makes all parameters as val, so we can have only accessors.
  • 43. Extending a Class • Just like java, classes can be extended using extends keyword. • Just like java, new methods and fields can be introduced or superclass methods or fields could be overridden in subclasses. • A class can be declared as final and avoid being overridden. •
  • 44. Extending a Final Class • Final class can not be overridden.
  • 45. Overriding Methods • Override modifier must be used to override an abstract methods. • Invoking superclass method is same as in java, by super keyword.
  • 46. Traits as Interface • Scala traits works exactly like java interface. • Methods need not be declared as abstract. • extend keyword is used to implement a trait in scala. • with keyword is used to extend more than one interface.
  • 48. Traits as Interface • Traits can have concrete and abstract methods. • If traits have a method with body, then the class which extends should use override keyword while extending it. • Traits cannot be instantiated. • Abstract method need a definition when a trait is extended. • Multiple traits can be overridden using with keyword. • A namespace collision is taken care by overriding the definition of the method with the last available definition.
  • 49. App trait • Since everything in scala is an object, even main method lives in a object. • There are two ways to create a launching point for your application: define an object that extends the App trait, or define an object with a properly defined main method. When using this approach, any command-line arguments to your application are implicitly available through an args object, which is inherited from the App trait. The args object is an instance of Array[String], just as if you had declared a main method yourself. The following code demonstrates how to use the args object: The second approach to launching an application is to manually implement a main method with the correct signature in an object, in a manner similar to Java:
  • 51. Functional Programming • Functional programming is mainly driven by • Functions are first class citizens. They are treated just as any other type say string, integer etc. so functions can be used as arguments , could be defined in other functions.
  • 52. Higher Order Functions Functions are first class citizens. They can be passed as a parameter and returned as a result. Functions that take other functions as parameter or that return functions as result are called Higher order functions.
  • 53. Examples 1. Decorator class has a primary constructor and takes 2 arguments as a parameter right and left of type String. 2. Decorator class has a function layout within. 3. layout accepts a parameter x which is of type A ( user defined type similar to T in java). [A] is type safety. 4. And layout returns a string which sandwiches argument x with left and right arguments. 1. Create a method with name apply which receives 2 parameter. Function f of type int , and v of type int. 2. F is an function which receives int as an argument and returns string as an value. 3. f : Int => String denotes function as a parameter. 4. Body of the function has f(v) which means the second argument v is passed as an argument to the function which was passed as an first argument. 1. Create an object to decorator class. 1. Call apply method and pass decorator.layout function as an argument and a number 7 as a value.
  • 54. Examples 2 This method has eliminated the need of sumInts, sumCubes and sumSquares function. Anonymous function has eliminated the need of other remaining functions sum, squares and cube as well. 5 functions are created. Lets see how we can minimize with help of higher order functions.