SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Scala
Session 4
Muhammad Asif Fayyaz
Course Contents
1. The Basics
2. Getting up to speed
3. Higher-Order functions
4. Classes and Objects
5. Inheritance and Traits
6. Collections
Today’s Session (Getting Up to Speed)
● First-Class Functions
● Function Types
● Anonymous Functions
● Shortening Anonymous Functions
● Place Holders
● Currying
● Tail Recursion
● Classes and Objects
Higher Order (First Class) Functions
- Scala treat functions as first-class values.
- This means that like any other value, a function can be
passed as a parameter and returned as a result.
- This provides a flexible way to compose programs.
- Functions that take other functions as parameters or
that return functions are called higher order functions
Example
Take the sum of the integers between a and b:
def id(x: Int) : Int = x
def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else id(a) + sumInts(a + 1, b)
Take the sum of the cubes of all the integers between a and b :
def cube(x: Int): Int = x * x * x
def sumCubes(a: Int, b: Int): Int =
if (a > b) 0 else cube(a) + sumCubes(a + 1, b)
Take the sum of the factorials of all the integers between a and b :
def fact(x: Int): Int = if (x ==0) 1 else x * fact(x-1)
def sumFactorials(a: Int, b: Int): Int =
if (a > b) 0 else fact(a) + sumFactorials(a + 1, b)
The code above looks very similar. May be we can improve it
Summing with Higher-Order Function
Let’s define:
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sum(f, a + 1, b)
We can then write:
def sumInts(a: Int, b: Int) = sum(id, a, b)
def sumCubes(a: Int, b: Int) = sum(cube, a, b)
def sumFactorials(a: Int, b: Int) = sum(fact, a, b)
where
def id(x: Int): Int = x
def cube(x: Int): Int = x * x * x
def fact(x: Int): Int = if (x == 0) 1 else x * fact(x - 1)
Function Types
- Types are defined as
- A => B
- This is a type of function that takes argument of
type A and return a result of type B
- Example
- Int => Int
- (Int, Int) => Int
- Lets have a look again at the example
Anonymous Functions
- Passing function as arguments lead to
defining many small functions.
- Tedious
- We should be able to do the same as
- def str = “Hello”; println(str)
- can be rewritten as println(“Hello”) using string literal
- These are called anonymous functions
Anonymous Function Syntax
- (x: Int) => x * x * x
- Takes parameter of type Int and return cube
- The type of parameter can be omitted if it can be inferred by the
compiler from the context
- If there are several parameters
- (x: Int, y: Int) => x + y
- An anonymous function (x1: T1,....xn:Tn) => E
can always be expressed using def
def f(x1: T1,....xn:Tn) = E; f
Anonymous Function Usage
- Heavily used in scala libraries
- Examples
- operations on data structures like List, Seq, Hashtable
- foreach
- filter
Summation with Anonymous Functions
- Using anonymous functions, we can write sums in a
shorter way:
def sumInts(a: Int, b: Int) = sum(x => x, a, b)
def sumCubes(a: Int, b: Int) = sum(x => x*x*x, a, b)
Shortening Anonymous Function
- Shortening the syntax
- Types can be omitted if can be inferred
- Using Placeholders (Further make the syntax concise)
Currying
Lets have a look again at sum function
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sum(f, a + 1, b)
def sumInts(a: Int, b: Int) = sum(x => x, a, b)
def sumCubes(a: Int, b: Int) = sum(x => x * x * x, a, b)
def sumFactorials(a: Int, b: Int) = sum(fact, a, b)
Note that (a,b) are passed unchanged from sumInts and sumCubes to sum
function.
Lets try to make this syntax even shorter
Currying (Cont…)
Lets rewrite sum as follows <<code>>
def sum(f: Int => Int): (Int, Int) => Int = {
def sumF(a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sum(f, a + 1, b)
sumF
}
Sum is now a function that returns another function
The returned function sumF applies the given function parameter f and sums
the results.
Currying -> Multiple Parameter Lists
- The definition of function that returns functions is so useful in functional
programming that there is a special syntax for it in Scala.
- For example, the following definition of sum is equivalent to the one with
the nested sumF function, but shorter:
def sum(f: Int => Int)(a: Int, b: Int): Int = {
if (a > b) 0 else f(a) + sum(f, a + 1, b)
def cube = (x: Int) => x * x * x
sum(cube)(1,3)
This style of carried functions is called currying
Tail Recursion
- Which of the implementation is better
def factRec(x: Int): Int = if (x == 0) 1 else x * factRec(x - 1)
def factIter(x: Int) = {
var result = 1
for (i <- x to 0 by -1) {
result *= i
}
result
}
- A tail call is a function call performed as the final action of the function.
- Tail recursion is a special case of recursion where the calling function does
no more computation after making a recursive call.
Classes and Objects
- Private members
- Creating and Accessing Objects
- Inheritance and Overriding
- Parameterless Methods
- Abstract Classes
- Traits
- Implementing Abstract Classes
- Dynamic Binding
- Objects
- Companion Objects
- Standard Classes
Partial Functions
Closure

Mais conteúdo relacionado

Mais procurados

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 

Mais procurados (20)

Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
Array within a class
Array within a classArray within a class
Array within a class
 
Templates2
Templates2Templates2
Templates2
 
Arrays
ArraysArrays
Arrays
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Arrays
ArraysArrays
Arrays
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 

Destaque

Alonzo Williams Resume.doc 1
Alonzo Williams Resume.doc 1Alonzo Williams Resume.doc 1
Alonzo Williams Resume.doc 1
Alonzo Williams
 
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòncông ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
lenore810
 
Ôm họa vì tăng cường “bản lĩnh đàn ông”
Ôm họa vì tăng cường “bản lĩnh đàn ông”Ôm họa vì tăng cường “bản lĩnh đàn ông”
Ôm họa vì tăng cường “bản lĩnh đàn ông”
len398
 
Professional Edition-Datasheet (3)
Professional Edition-Datasheet (3)Professional Edition-Datasheet (3)
Professional Edition-Datasheet (3)
davidgbz
 

Destaque (13)

Alonzo Williams Resume.doc 1
Alonzo Williams Resume.doc 1Alonzo Williams Resume.doc 1
Alonzo Williams Resume.doc 1
 
Notka
NotkaNotka
Notka
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòncông ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
công ty dịch vụ giúp việc theo giờ giá rẻ nhất ở sài gòn
 
Santiago
SantiagoSantiago
Santiago
 
Web page evaluation
Web page evaluationWeb page evaluation
Web page evaluation
 
Ôm họa vì tăng cường “bản lĩnh đàn ông”
Ôm họa vì tăng cường “bản lĩnh đàn ông”Ôm họa vì tăng cường “bản lĩnh đàn ông”
Ôm họa vì tăng cường “bản lĩnh đàn ông”
 
098765
098765098765
098765
 
Colegio nacional nicolas esguerra 1
Colegio nacional nicolas esguerra 1Colegio nacional nicolas esguerra 1
Colegio nacional nicolas esguerra 1
 
Professional Edition-Datasheet (3)
Professional Edition-Datasheet (3)Professional Edition-Datasheet (3)
Professional Edition-Datasheet (3)
 
Los instrumento musicales
Los instrumento musicalesLos instrumento musicales
Los instrumento musicales
 
La MúSica De La India
La MúSica De La IndiaLa MúSica De La India
La MúSica De La India
 
Souvenir kantor pajak profile
Souvenir kantor pajak profileSouvenir kantor pajak profile
Souvenir kantor pajak profile
 

Semelhante a Lecture 4

Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
Maxim Zaks
 

Semelhante a Lecture 4 (20)

Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
Practical cats
Practical catsPractical cats
Practical cats
 
Composition birds-and-recursion
Composition birds-and-recursionComposition birds-and-recursion
Composition birds-and-recursion
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
C# programming
C# programming C# programming
C# programming
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

Lecture 4

  • 2. Course Contents 1. The Basics 2. Getting up to speed 3. Higher-Order functions 4. Classes and Objects 5. Inheritance and Traits 6. Collections
  • 3. Today’s Session (Getting Up to Speed) ● First-Class Functions ● Function Types ● Anonymous Functions ● Shortening Anonymous Functions ● Place Holders ● Currying ● Tail Recursion ● Classes and Objects
  • 4. Higher Order (First Class) Functions - Scala treat functions as first-class values. - This means that like any other value, a function can be passed as a parameter and returned as a result. - This provides a flexible way to compose programs. - Functions that take other functions as parameters or that return functions are called higher order functions
  • 5. Example Take the sum of the integers between a and b: def id(x: Int) : Int = x def sumInts(a: Int, b: Int): Int = if (a > b) 0 else id(a) + sumInts(a + 1, b) Take the sum of the cubes of all the integers between a and b : def cube(x: Int): Int = x * x * x def sumCubes(a: Int, b: Int): Int = if (a > b) 0 else cube(a) + sumCubes(a + 1, b) Take the sum of the factorials of all the integers between a and b : def fact(x: Int): Int = if (x ==0) 1 else x * fact(x-1) def sumFactorials(a: Int, b: Int): Int = if (a > b) 0 else fact(a) + sumFactorials(a + 1, b) The code above looks very similar. May be we can improve it
  • 6. Summing with Higher-Order Function Let’s define: def sum(f: Int => Int, a: Int, b: Int): Int = if (a > b) 0 else f(a) + sum(f, a + 1, b) We can then write: def sumInts(a: Int, b: Int) = sum(id, a, b) def sumCubes(a: Int, b: Int) = sum(cube, a, b) def sumFactorials(a: Int, b: Int) = sum(fact, a, b) where def id(x: Int): Int = x def cube(x: Int): Int = x * x * x def fact(x: Int): Int = if (x == 0) 1 else x * fact(x - 1)
  • 7. Function Types - Types are defined as - A => B - This is a type of function that takes argument of type A and return a result of type B - Example - Int => Int - (Int, Int) => Int - Lets have a look again at the example
  • 8. Anonymous Functions - Passing function as arguments lead to defining many small functions. - Tedious - We should be able to do the same as - def str = “Hello”; println(str) - can be rewritten as println(“Hello”) using string literal - These are called anonymous functions
  • 9. Anonymous Function Syntax - (x: Int) => x * x * x - Takes parameter of type Int and return cube - The type of parameter can be omitted if it can be inferred by the compiler from the context - If there are several parameters - (x: Int, y: Int) => x + y - An anonymous function (x1: T1,....xn:Tn) => E can always be expressed using def def f(x1: T1,....xn:Tn) = E; f
  • 10. Anonymous Function Usage - Heavily used in scala libraries - Examples - operations on data structures like List, Seq, Hashtable - foreach - filter
  • 11. Summation with Anonymous Functions - Using anonymous functions, we can write sums in a shorter way: def sumInts(a: Int, b: Int) = sum(x => x, a, b) def sumCubes(a: Int, b: Int) = sum(x => x*x*x, a, b)
  • 12. Shortening Anonymous Function - Shortening the syntax - Types can be omitted if can be inferred - Using Placeholders (Further make the syntax concise)
  • 13. Currying Lets have a look again at sum function def sum(f: Int => Int, a: Int, b: Int): Int = if (a > b) 0 else f(a) + sum(f, a + 1, b) def sumInts(a: Int, b: Int) = sum(x => x, a, b) def sumCubes(a: Int, b: Int) = sum(x => x * x * x, a, b) def sumFactorials(a: Int, b: Int) = sum(fact, a, b) Note that (a,b) are passed unchanged from sumInts and sumCubes to sum function. Lets try to make this syntax even shorter
  • 14. Currying (Cont…) Lets rewrite sum as follows <<code>> def sum(f: Int => Int): (Int, Int) => Int = { def sumF(a: Int, b: Int): Int = if (a > b) 0 else f(a) + sum(f, a + 1, b) sumF } Sum is now a function that returns another function The returned function sumF applies the given function parameter f and sums the results.
  • 15. Currying -> Multiple Parameter Lists - The definition of function that returns functions is so useful in functional programming that there is a special syntax for it in Scala. - For example, the following definition of sum is equivalent to the one with the nested sumF function, but shorter: def sum(f: Int => Int)(a: Int, b: Int): Int = { if (a > b) 0 else f(a) + sum(f, a + 1, b) def cube = (x: Int) => x * x * x sum(cube)(1,3) This style of carried functions is called currying
  • 16. Tail Recursion - Which of the implementation is better def factRec(x: Int): Int = if (x == 0) 1 else x * factRec(x - 1) def factIter(x: Int) = { var result = 1 for (i <- x to 0 by -1) { result *= i } result } - A tail call is a function call performed as the final action of the function. - Tail recursion is a special case of recursion where the calling function does no more computation after making a recursive call.
  • 17. Classes and Objects - Private members - Creating and Accessing Objects - Inheritance and Overriding - Parameterless Methods - Abstract Classes - Traits - Implementing Abstract Classes - Dynamic Binding - Objects - Companion Objects - Standard Classes