SlideShare a Scribd company logo
1 of 64
https://in.linkedin.com/in/sujithsudhakaran
An Introduction
to
By
Sujith Sudhakaran
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
● Renaissance of Functional Programming
● Scala Intro
● Concepts
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
– Understanding our usual style
– Bottle neck – Moore's Law
– Concurrency and Parallel Programming
● Renaissance of Functional Programming
● Scala Intro
● Concepts
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Imperative Programming...
https://in.linkedin.com/in/sujithsudhakaran
Imperative clichés...
https://in.linkedin.com/in/sujithsudhakaran
Imperative clichés...
WHY?
https://in.linkedin.com/in/sujithsudhakaran
Moore’s Law
https://in.linkedin.com/in/sujithsudhakaran
Concurrency
https://in.linkedin.com/in/sujithsudhakaran
Parallel programming
https://in.linkedin.com/in/sujithsudhakaran
Concurrency & Parallel
programming
Really HARD
to get it RIGHT
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
● Renaissance of Functional Programming
– Non determinism and side effects
– Functional programming
● Scala Intro
● Concepts
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Non-determinism
var x = 0
async { x = x + 3; println(x)}
async { x = x * 5; println(x)}
https://in.linkedin.com/in/sujithsudhakaran
What's happenning here?
https://in.linkedin.com/in/sujithsudhakaran
What's happenning here?
Side effects
https://in.linkedin.com/in/sujithsudhakaran
Resolving the side effect...
https://in.linkedin.com/in/sujithsudhakaran
What we learnt so far...
● Immutability
● Pure functions
https://in.linkedin.com/in/sujithsudhakaran
What is Functional
Programming?
https://in.linkedin.com/in/sujithsudhakaran
OOPs!!
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
● Renaissance of Functional Programming
● Scala Intro
– Scala as unifier
– Class hierarchy
– Keywords used
– Scala being concise
● Concepts
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Scale Language - Introduction
https://in.linkedin.com/in/sujithsudhakaran
Scala – Class Hierarchy
https://in.linkedin.com/in/sujithsudhakaran
Scala – keywords used in the
examples
● val: to declare a immutable variable
● var: to declare a mutable variable
● def: to define a function
● println: to print out line
● trait: similar to interfaces in java
● sealed: keyword used to specific classes locally only
● object: keyword used to instantiate a singleton object
● extends: used for inheritence
● with: used while doing multiple inheritence
● match: keyword for pattern matching
● case: used for pattern matching
● override: used to override a behavior of an interface
● =>: used to show the some input mapping to output
https://in.linkedin.com/in/sujithsudhakaran
Scala being concise...
Type inference
https://in.linkedin.com/in/sujithsudhakaran
Scala being concise...
High-level
https://in.linkedin.com/in/sujithsudhakaran
Scala being concise...
High-level
https://in.linkedin.com/in/sujithsudhakaran
Scala being concise...
Less boiler plate
https://in.linkedin.com/in/sujithsudhakaran
Scala being OO...
Every value is an object
– Eg: 1 // Yes, its an object
– 1.toString // method can be called
– 1 + 2 + 3 is equivaled to (1).+(2).+(3)
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
● Renaissance of Functional Programming
● Scala Intro
● Concepts
– Call by value/Call by name
– Higher order functions
– Tail recursion
– Traits
– Abstract Types
– Partial functions
– Pattern matching
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Call by value/Call by name
Call by value
https://in.linkedin.com/in/sujithsudhakaran
Call by value/Call by name
The problem we may face...
https://in.linkedin.com/in/sujithsudhakaran
Call by value/Call by name
Call by name
https://in.linkedin.com/in/sujithsudhakaran
Functions
● First class values
● Function type S => T is
trait Function1[-S, +T] {
def apply(x: S): T
}
● def sum(x: Int, y: Int) = x + y
● sum(1, 2)
● (x: Int, y: Int) => x + y // What is the use of this function?
https://in.linkedin.com/in/sujithsudhakaran
Higher order functions
What’s common in this two programs?
https://in.linkedin.com/in/sujithsudhakaran
Higher order functions
https://in.linkedin.com/in/sujithsudhakaran
Higher order functions
● Examples: map, filter, reduce etc.
https://in.linkedin.com/in/sujithsudhakaran
Tail recursion
What's the difference in these programs?
> f (21 == 0) 14 else gcd(21, 14 %21)
> if (false) 14 else gcd(21, 14 %14)
> gcd(21, 14 %21)
> gcd(21, 14)
> if (14 == 0) 21 else gcd(14, 21 %14)
> gcd(14, 7)
> gcd(7, 0)
> 7
> if (5 == 0) 1 else 5 * fact(5-1)
> 5 * fact(4)
> 5 * (4 * fact(3))
> 5 * (4 * (3 * (fact(2))))
> 5 * (4 * (3 * (2 * fact(1))))
> 5 * (4 * (3 * (2 * (1 * fact(0))))))
> 5 * (4 * (3 * (2 * (1 * 1)))))
> 120
https://in.linkedin.com/in/sujithsudhakaran
Tail recursion
What's the difference in these two example?
> f (21 == 0) 14 else gcd(21, 14 %21)
> if (false) 14 else gcd(21, 14 %14)
> gcd(21, 14 %21)
> gcd(21, 14)
> if (14 == 0) 21 else gcd(14, 21 %14)
> gcd(14, 7)
> gcd(7, 0)
> 7
> if (5 == 0) 1 else 5 * fact(5-1)
> 5 * fact(4)
> 5 * (4 * fact(3))
> 5 * (4 * (3 * (fact(2))))
> 5 * (4 * (3 * (2 * fact(1))))
> 5 * (4 * (3 * (2 * (1 * fact(0))))))
> 5 * (4 * (3 * (2 * (1 * 1)))))
> 120
https://in.linkedin.com/in/sujithsudhakaran
Tail recursion
The Problem!!
https://in.linkedin.com/in/sujithsudhakaran
Tail recursion
The Problem!!
StackOverflowError Exception
https://in.linkedin.com/in/sujithsudhakaran
Tail recursion - Solution
https://in.linkedin.com/in/sujithsudhakaran
Traits - Mixin class composition
● Share interfaces and fields between classes
● Can be extended but NOT instantiated
● Use the keyword with to extend from multiple traits
● Traits with code are known as Mixins
● Abstract and concrete fields
https://in.linkedin.com/in/sujithsudhakaran
Traits – Multiple Inheritence(Diamond
Problem)
Diamond Problem
Staff
Person
Student Teacher
Student
Teacher
https://in.linkedin.com/in/sujithsudhakaran
Traits – Solving Diamond Problem
Linearization
https://in.linkedin.com/in/sujithsudhakaran
Traits – Solving Diamond Problem
Linearization
Derived
B
C
Base
AnyRef
Any
A
Base
AnyRef
Any
https://in.linkedin.com/in/sujithsudhakaran
Traits – Solving Diamond Problem
Linearization
ABCDerived Base AnyRef Any
https://in.linkedin.com/in/sujithsudhakaran
Abstract Types
The Problem
https://in.linkedin.com/in/sujithsudhakaran
Abstract Types
The solution
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching
● How we used switch statements?
● Generalization of switches
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching
● How we used switch statements?
● Generalization of switches
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching
● How we used switch statements?
● Generalization of switches
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching – More
examples
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching – More
examples
https://in.linkedin.com/in/sujithsudhakaran
Partial functions
● In FP, Input => Output
● Form:
val divide = new PartialFunction[Int, Int] {
def apply(x: Int, y: Int) = x / y
def isDefinedAt(x: Int, y: Int) = y != 0
}
● def collect[B](pf: PartialFunction[A, B])
https://in.linkedin.com/in/sujithsudhakaran
Partial functions - Example
Guess the type
https://in.linkedin.com/in/sujithsudhakaran
Partial functions - Example
https://in.linkedin.com/in/sujithsudhakaran
Partial functions - Example
https://in.linkedin.com/in/sujithsudhakaran
Pattern matching & Partial
Funtion
More examples
https://in.linkedin.com/in/sujithsudhakaran
To summarize...
● Compatible
● OOP
● Functional
● Concise
● High level abstractions over data structures
● Compose functions
● Statically typed
● Extensible
https://in.linkedin.com/in/sujithsudhakaran
Agenda
● Imperative Programming
● Renaissance of Functional Programming
● Scala Intro
● Concepts
● Popular frameworks
https://in.linkedin.com/in/sujithsudhakaran
Popular frameworks
● Apache Spark
● Akka
● Play Framework
● Other trending projects in scala
https://in.linkedin.com/in/sujithsudhakaran
Who is using Scala?
https://in.linkedin.com/in/sujithsudhakaran
References
● Martin Odersky, "Working Hard to Keep It Simple" -
OSCON Java 2011
● Martin Odersky: Scala with style
● DSL: Writing web framework router
● State you're doing it wrong
● Origin of scala
https://in.linkedin.com/in/sujithsudhakaran
Questions!!
https://in.linkedin.com/in/sujithsudhakaran
Thank You!

More Related Content

Similar to Introduction to Scala

10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rockmartincronje
 
Introduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsIntroduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsMindfire Solutions
 
Connascence - How to detect code smell
Connascence - How to detect code smellConnascence - How to detect code smell
Connascence - How to detect code smellThành ✐
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastHolden Karau
 
Guide to wall street quant jobs for IITians
Guide to wall street quant jobs for IITiansGuide to wall street quant jobs for IITians
Guide to wall street quant jobs for IITiansPratik Poddar
 
Using Scrum on 3SL Cradle - traceability model and project schema
Using Scrum on 3SL Cradle - traceability model and project schemaUsing Scrum on 3SL Cradle - traceability model and project schema
Using Scrum on 3SL Cradle - traceability model and project schemaYulia Madorskaya
 
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...WebStackAcademy
 
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Holden Karau
 
Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Holden Karau
 
Thinking in data structures
Thinking in data structuresThinking in data structures
Thinking in data structuresTushar B Kute
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patternsPascal Larocque
 
Make it Responsive! the logic, the code & tricks of trade
Make it Responsive! the logic, the code & tricks of tradeMake it Responsive! the logic, the code & tricks of trade
Make it Responsive! the logic, the code & tricks of tradeSidharth Sidharth
 
Specifications pattern
Specifications patternSpecifications pattern
Specifications patternTung Nguyen
 
ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIMichael Remijan
 
3D Design with OpenSCAD
3D Design with OpenSCAD3D Design with OpenSCAD
3D Design with OpenSCADVickyTGAW
 
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar Applitools
 
Improving PySpark performance: Spark Performance Beyond the JVM
Improving PySpark performance: Spark Performance Beyond the JVMImproving PySpark performance: Spark Performance Beyond the JVM
Improving PySpark performance: Spark Performance Beyond the JVMHolden Karau
 
PyConUK2013 - Validated documents on MongoDB with Ming
PyConUK2013 - Validated documents on MongoDB with MingPyConUK2013 - Validated documents on MongoDB with Ming
PyConUK2013 - Validated documents on MongoDB with MingAlessandro Molina
 
The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...Holden Karau
 

Similar to Introduction to Scala (20)

10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rock
 
Introduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsIntroduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.js
 
Connascence - How to detect code smell
Connascence - How to detect code smellConnascence - How to detect code smell
Connascence - How to detect code smell
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at last
 
Guide to wall street quant jobs for IITians
Guide to wall street quant jobs for IITiansGuide to wall street quant jobs for IITians
Guide to wall street quant jobs for IITians
 
Using Scrum on 3SL Cradle - traceability model and project schema
Using Scrum on 3SL Cradle - traceability model and project schemaUsing Scrum on 3SL Cradle - traceability model and project schema
Using Scrum on 3SL Cradle - traceability model and project schema
 
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
 
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
 
Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016
 
Thinking in data structures
Thinking in data structuresThinking in data structures
Thinking in data structures
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patterns
 
Make it Responsive! the logic, the code & tricks of trade
Make it Responsive! the logic, the code & tricks of tradeMake it Responsive! the logic, the code & tricks of trade
Make it Responsive! the logic, the code & tricks of trade
 
Specifications pattern
Specifications patternSpecifications pattern
Specifications pattern
 
ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming API
 
3D Design with OpenSCAD
3D Design with OpenSCAD3D Design with OpenSCAD
3D Design with OpenSCAD
 
Going web native
Going web nativeGoing web native
Going web native
 
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar
Collaborating with Developers: How-to Guide for Test Engineers - By Gil Tayar
 
Improving PySpark performance: Spark Performance Beyond the JVM
Improving PySpark performance: Spark Performance Beyond the JVMImproving PySpark performance: Spark Performance Beyond the JVM
Improving PySpark performance: Spark Performance Beyond the JVM
 
PyConUK2013 - Validated documents on MongoDB with Ming
PyConUK2013 - Validated documents on MongoDB with MingPyConUK2013 - Validated documents on MongoDB with Ming
PyConUK2013 - Validated documents on MongoDB with Ming
 
The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...
 

Recently uploaded

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 

Recently uploaded (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 

Introduction to Scala

Editor's Notes

  1. - Introduce myself - My background - Share my learning and head start Questions: - Java backgroung - worked on func language - My journey with Scala - Realization - Point out the difference and similarities
  2. - Brief idea about the things to cover - Talk about the things from the slide
  3. - So lets dive in
  4. - Question: what is IP? - Show the image - What is to be computed is interwined with how it is to be computed - Focus is on how the program operates - Consists of commands for computer - Always try to think w.r.t to time
  5. - Show the examples - I think everyone one of us might have written such code Haven’t you? Typically, IP - Changes the program state - Assignments, for/while loops, goto, conditional statements - Carrying on the legacy - Eg: C, C++, Java, Go, Python, Ruby etc. - 3rd example: Any problem? - We’ll about the problem later - why are we doing this?
  6. Next question: - Why ? - HW limitations made us to be imperative - Optimize code - No extra memory - Review comments - Since memory on chip was less and very expensive - Inherently Imperative - Why things have changed now?
  7. Question: How many of you know what’s Moore’s Law Gordon Moore 1965 - Number of transistors per square inch on integrated circuits had doubled every year since their invention Show the graph Explain the graph - It’s become dead - More cores Questions: Are we taking advantage of such Hws? Not the HW we have asked for Difficult to unlearn the things which we were doing in IP Thoughts from Martin Every IP languague has tried to take adv Next slide
  8. Before the image Question: - What is concurrency? - Way to acheive faster programs - No dependency on order of execution - Has it’s own problem - Show the image - deadlocks, race conditions etc Q & A So, How does scala achieves it? - Actor model Typically 4 ways Shared state Software Transaction memory Message Passing Dataflow concurrency
  9. Before image Question: - What is Parallel programming? - Execute programs faster on different hws - Programs can be sequential in nature - Show the image - Its very good when we have such infra; but are we ready to take the problems while this is getting built - So, we have the multi-core Hws - Are we able to take advantage of it - Difficult if left to developers - Extra baggage of imperative style - Its difficult to handle synchronization - Languagues should be inherently design to make use multi core hws - Scala does it with the help of mutable Hws - We can simply call .par function on the collections - Provided we follow principles of FP which we will talk about in a while
  10. - So, if we stick to our conventional IP - Its really hard to get it right - You will definitely lates nights debugging production issues
  11. - FP is not something which is new - It was there all this time and was more popular in academics - But now we are seeing rebirth of FP in various languaes like - Haskell(Best) - Lisp - Erlang - Scala etc. - Lets try to understand the building block of FP
  12. Question: Can we be certain about the outcome of this code? Why? - The problem is the shared state - Mutex, locks etc - But, why make our lives miserable - Culprit - Why not get rid of the culprit itself - Immutability - Lets have a look at the next pillar
  13. - Remember this eg from previous slide - What’s the problem - How about this example? - They have side effects
  14. - They both are impure functions - Anything which changes some state outside it’s scope - So, understanding and debugging such a fuction requires knowledge about context and its possible history Question - Production bug scenario? - Developer cannot reproduce
  15. - Pure functions makes systems to be - easier to understand - easier to analyze - Easier to test - Easier to optimize - From the previous example, Isn’t this code more convinient to test - Scala doesn’t restrict side effects it advocates to be - explicit - declarative - rather than allowing users to assume - So, that’s the ideology we should keep in mind
  16. - As we saw from last couple of side - Immuatability & Pure F are building blocks for FP - Stick to this 2 pillars to build any sort of complex concurrent and parrallel apps
  17. Question: What is this? - Show the image - Functions should be mapping input values to o/p not change any state - Why do I have a octopus kind of creature here? - Just to depict the scenario that it’s very simple to use with parallel programming - Primary concepts - Immutability for data structures - Pure functions without side-effects - Auxiliary concepts - Recursion - First class and higher order functions - Type systems - Referential transparency - So, if we follow this style, eventually our code becomes - elegant - readable - concise - Free from side effects or inconsistent results - So, why should we do this? - CPU are not getting faster anymore and memory is getting cheaper - Simpler reasoning principles - Better modularity and abstractions - Transparent concurrent code - More maintainable and bug free code - New perspective for problems - Leave all these reasons aside, It will be a new weapon to your arsenal
  18. Before the image Question - How many of you like OOPs? - Advantages: - Abstraction - Modularity - Code reuse - Clear defined interfaces - Easy to maintain - Suitable for larger projects
  19. So, we have talked about Imperative, Functional and OOP. Now, we are going to see where does Scala fits in all of this FACADE
  20. History before the image Pizza before Scala 3 features from FP: - Generics - Higher Order functions - Pattern matching - This formed the basis for Generics in Java 5 - Current era, one size doesn’t fit all - Hence, Scala has taken an hybrid approach - Show the image - Explain the image - Express common programming patterns in a concise, elegant, and type-safe way. - It's an attempt to acheive smoother integration between FP ana OOP - Martin Ordersky, 2004 - Statically typed - Mix of OOP and FP - Runs on JVM - Inter-operable with Java - Concise - High-level
  21. - All values have a type - Including numerical values and functions - Any: supertype of all types - AnyVal -> value types - AnyRef -> reference types - This corresponds to java.lang.Object - So that’s how Scala maintains inter-operability with Java classes
  22. - Read it out
  23. - Before the images - Omit certain type annotations - Compiler can deduce the type from the type of the initialization expression - Same implies with function return types - 1 Exception though, Recursive functions - Show the java example - Show the Scala example
  24. - Emphasize on how it’s written in Java and Scala - Next slide is also about high level
  25. Q & A: - How many of you like to write the access specifiers in other languaes and other boiler plate code - Atleast, now I don’t - Scala, has made my life way simpler
  26. - All the advantages we talked about OOP is readily available with Scala - Every value is object - As in the examples We can have things like: - Classes - Objects - Abstract classes - Traits - Case classes etc - But don’t use the Scala classes as java classes - Object: instantiated once, singleton, all members static - Abstract classes: similar to java, only subclassed - Traits are akin to Interfaces - Case Classes: immutable data holding entities that are used for pattern matching; no new keyword; has companion objects that are automatically crated which serve as the extractors; apply and unapply methods - That’s how elegant is Scala in OO domain
  27. - next we’ll talk about the concepts which i have used and felt interesting - There are many other concepts like - Currying - Self types - Annotations - Generics etc - We won’t be though them in this session atleast
  28. - Before the image -Doing this for quiet some time now. This is still the same in Scala. - So, every value is evaluated when they are passed around - In IP, we had taken it further by having pass by reference. But, we are not going to talk about it here - Scala being expression oriented language, it promotes pass by value too - Show the image - Explain what's happening in the program
  29. - Before the image - But there is a catch!! - Lets look a problem which we may encounter when we use pass by value Since, in scala everything is value. Right? - show the image - Discuss the problem
  30. With only 1 image - Take a look at this scala code - Talk about the notation(syntax) - Show the 2nd image - Talk about the side effect now because of the t variable - There is a catch in this function. This version is not a pure function - Either way works for Scala provided - Reduced expressions consists of pure functions - Both evaluations terminates - Call by value - Represented like a val - Every function argument is evaluated only once - Call by name - Represented like def - Argument is not evaluated if the param is not used
  31. - Since every value is an object - They are objects with apply method - As we pass x and y to the sum function; we can pass around the sum function too - Show the anonymous functions - since its a value we can assign it to a variable - Question: - What is a diff between methods and functions? - Techincally, they are considered differently in scala, I haven’t told you how? yet, - Guess representation for funciton and method - Reason, since if we use def it’s an object and it’s like calling apply method on that obj
  32. - Before the image - HOF are functions that take or return an another function - Lets look at some example where we will use them - Show the image 1 - Talk about the example - Show the image 2 - Talk about the example - Question - What’s common - functionality of addition is common - What’s different - What needs to be added - Design Principle: “Separate what cahnges from what stays the same” - So, lets try to do that
  33. - Expalin the example Question: - Can we do better?
  34. - Explain the example - Means for abstraction and creating new control structures - What I have learnt is that it's far more easier to take in a function rather than returning a function - Next concept which we are going to see is from the family of Recursion
  35. - Before the images - Ask about how many like recursion? - Personally I never liked it; difficult for me - show the image - Talk about the example; exapsion - Show next image - Talk about the example; exapsion Quetions: What’s the difference - We add 1 more value to our expression - last statement of the function is a call to itself - then the same stack frame can be used and such calls are know as tail calls - Executes in constant space - The stack frame size is not increased by adding another function jump
  36. So, with the GCD example compiler is able to figure that out - Hence, it says, tail recursive - Whereas, factorial is recursive call - So, what’s the harm? It should be fine.
  37. - Explain the 1st image - Show the problem in the second image - the problem here is because of the JVM stack frame size - Go to next slide
  38. So, now what? - Do I have to always explode my programs? - No, so now we write tail recursive calls - Go to next slide
  39. - Explain the example - This is all good but I agree that it make your code a bit complex. So, if you know for sure you won't hit the max stack frame size of JVM then it's always better to use normal recursion
  40. - Read the points from slide - Similar to the Java interfaces - Offers more than interfaces - only normal constructors - Can't have auxiliary constructors - We'll see example and usage of Abstract type - Use Scala traits as Java Abstract class
  41. - Before the rigth image - Talk about the diamond problem - Talk about various approaches in other languages - using virtual in C++ while inheriting - Application for a college with various role - Helps in having multiple inheritence - We'll see how it's done in Scala
  42. - Scala has created strict rules for multiple inheritence - Class hierarchy creates a acyclic graph of inheritence - Because of such strict rules there is no confusion in which function should be called
  43. - The graph here is for just illustration only - Linearization has a lot to do with the order in which the traits are mixed - Prev example: Derived was extending A and C - Explain the acyclic graph formation - Explain how it will be reduced and then just show the image on next slide
  44. - Ask if any questions?
  45. - So, I told that we would be talking about abstract types. Right? - Lets look at this problem here - Explain the problem - Talk about the solution by Anil
  46. - Explain the solution here - There is more to the abstract types/generics with variance and covariance - I think you get the gist of what Scala can offer in generics - Covariance +A -> pass any subtype of A - For some class List[+A], A is subtype of B then List[A] is a subtype of List[B] - Contravariant -A -> we can pass any parent - Writer[-A]: A is subtype of B, then Writer[B] is a subtype of Writer[A]
  47. After 1st point Question: - How many of you like switch statements? - if-else/switch - Wherever possible i use cases; reason it looks somewhat good and get rid of extra equality check - Clatter when variable name is large - Traditional for constant value only - 2nd point - Show the examples - Match is a keyword and used as an expression - Must yield a result - No case matches, exception(MatchError) - Go through the slide to show the examples (2 slides)
  48. - Explain what’s happening
  49. - Explain what’s happening - Talk about case classes: - Almost like classes with some extra properties - No new keyword req - All case classes have so called companion objects; automatically created - They have apply and unapply methods - If using abstract class like in the example - How to ensure all cases are covered; someone can extend this class anywhere else - Sealed classes: - Need to mention in that file only - cover all possible classes
  50. - Explain the example - Talk about pattern guard - We will get back to this example later too
  51. - Truth table for tuple classes - Imagine you writing if-else for this truth table - Code is more concise, clean and readable
  52. - Question before content - Any idea what do they mean? - Pure Functions work on their argument and yield a result - What if there are result for only range of input - Use if-else but we have to return something for that range right? - show the next content - Talk about the example - Talk about the collection function - Comes in handy for data analysis programs - Various types of data; for the program we are concerned about very specific type - Can’t go on adding if-else right?
  53. - Guess the type - Explain the example - Show the type Question - What will happen when I uncomment the map function?
  54. - Error! - Why Error, explain - Lets see how can be do it in a better way!
  55. - With collect being a PF - I have defined my function only for Ints; Hence the result would be a list of Int - Now, the multiple option is available on Int; hence the result
  56. - Just for illustration only - Defiining a PF - Last arg is the return type - Remember the example on list, where we had to handle all the cases of Nil and non empty
  57. - Scala is compatible with Java and it can make use of the vast extensive Java libraries - Being OO, all the advantages are available for us but with a pinch of salt - Usage is different from that of traditional OO Being functional, we have an opportunity to create clear, transparent and complex concurrent applications - Our code becomes: - short - easier to understand - less boiler plate code, type inference - extensive abstraction over data structures - Compose and make your own abstractions and control structures - Being statically typed - Sure about refactorings Scala provides opportunities to build your own DSL - Design you own constructs
  58. Actors For comprehension Generics Extractors Annotations Self types(cake pattern) to remove DI(Dependency Injection) Currying Type system Type specialization XML Literals DSLs Scripting with Scala Etc.
  59. - Spark: - It's an OS cluster computing framework - API's to program with implicit data parallelism and fault tolerance - They have this concept of RDDs(Resilient distributed dataset), which are immutable in itself - All you can do with the dataset is to transform it or apply some actions on it - Hence, they are inherently parallel and fault tolerant - Generally, used in Data analysis needs - Akka: - If your business needs is to have many concurrent tasks and they have to synchronize between each other, then AKKA is you one-stop destination - Set of OS libraries to design scalable, resilient systems that span across cores and network - Gone are the days of low level mutex, atomics or locks - All the overhead of handling concurrency is taken care by it - Their core concept is Actor model, adhering to some simple pattern we are easily able to write transparent and concurrent code - There are many packages - FSM (I'm using this in my project) - Akka Routes - Akka Cluster - Akka Persistence - Akka Streams - All of them are based on Actor model - So, you guys go and read about Actor model - Play - How many of you have build Java web applications? - This is something for you guys to explore - Framework for building web applications using Scala and Java written in Scala - It's also builtN on Akka - It's totally stateless - Integrated unit testing; support for JUnit and Selenium
  60. - There are many big players who are using Scala - So, if you look at the future of Scala, I think It's just warming up before the actual the actual game!!