SlideShare a Scribd company logo
Programming in Scala
Lecture Three
Angelo Corsaro
13 December 2017
Chief Technology Officer, ADLINK Technology Inc.
Table of contents
1. Reviewing Fold
2. More on Scala Classes
3. Algebraic Data Types
4. Case Classes
and
Pattern Matching
5. For Expression Revisited
6. Type Parameterization
1
Reviewing Fold
foldr and foldl
1 def reverse(xs: List[Int]): List[Int] = xs match {
2 case List() => List()
3 case y::ys => reverse(ys) ::: List(y)
4 }
5
6
7 val xs = List(1,2,3,4,5)
8 val ys = reverse(xs)
9
10 val zs = ys.foldLeft(List[Int]())((xs: List[Int], x: Int) => x :: xs)
11
12 val ks = zs.foldRight(List[Int]())((x: Int, xs: List[Int]) => xs ::: List(x) )
2
Visualizing folds
3
More on Scala Classes
Scala Classes Refresher
In the first lecture we saw the basic mechanism provided by Scala to declare
Classes. Back than we had seen closures without realizing it, thus let’s take a
fresh look at some of the examples:
1 class Complex(val re: Double, val im: Double) {
2 override def toString = s"$re+i$im"
3 }
4
5 class Rational(a: Int, b: Int) {
6 assert(b != 0)
7 val num: Int = a / gcd(a, b)
8 val den: Int = b / gcd(a, b)
9
10 def this(n: Int) = this(n, 1)
11 }
4
Abstract Classes
In Scala the abstract modifier is used to denote an abstract class. In other
terms, a class that cannot be instantiated.
An abstract class may have abstract members that don’t have an implementation
(definition).
1 package edu.esiee.scala17.ex1
2
3 abstract class Shape {
4 def scale(a: Float): Shape
5
6 // Parameter-less methods: Two takes:
7 def show(): Unit
8
9 def area: Float
10 def perimeter: Float
11
12 }
5
Parameter-less Methods
Parameter-less methods can be declared with or without parenthesis.
The convention is that parenthesis are used for methods that cause side-effects –
like show above.
Parenthesis are not used to methods that compute properties. This way there
uniform access between attributes and methods.
6
Extending a Class
The Shape abstract type had defined some of the characteristics shared by some
geometrical shapes.
A concrete shape can be defined by extending the abstract Shape class as follows:
1 package edu.esiee.scala17.ex1
2
3 class Circle(val radius: Float) extends Shape {
4
5 def scale(a: Float): Shape = new Circle(a * radius)
6
7 def show(): Unit = print(s"Circle($radius)")
8
9 def area: Float = Math.PI.toFloat*radius*radius
10 def perimeter: Float = 2*Math.PI.toFloat*radius
11 }
7
Methods or Vals?
The area and perimeter have been defined as parenthesis less methods.
As our shape are immutable, a valid question is if we could define them with vals
and compute them only once, as opposed to every-time the method is called.
The good news is that Scala supports the concept of abstract val, let’s see how
to leverage them.
8
Abstract val
Along with abstract methods, an abstract class can also have abstract vals.
These are values which are declared but not defined.
If we were to leverage abstract values our Shape class could be declared as
follows:
1 package edu.esiee.scala17.ex2
2
3 abstract class Shape {
4 def scale(a: Float): Shape
5
6 def show(): Unit
7
8 val area: Float
9 val perimeter: Float
10
11 }
9
Implementing abstract val
Implementing an abstract val is as simple ad defining it as show in the code
example below:
1 package edu.esiee.scala17.ex2
2
3 class Circle(val radius: Float) extends Shape {
4
5 def scale(a: Float): Shape = new Circle(a * radius)
6
7 def show(): Unit = print(s"Circle($radius)")
8
9 val area: Float = Math.PI.toFloat*radius*radius
10 val perimeter: Float = 2*Math.PI.toFloat*radius
11 }
10
Observation
Now, as we are using val the shape area is computed only once, at the time of
initialization. This is an improvement compared from the previous example.
Additionally, the client code cannot tell whether we are implementing the area
and the perimeter with a val or with a parenthesis less method which is very
good.
Yet... We are still spending the time even if the client code is never calling that
operation. In an ideal world we would want to compute it once and only if
needed... Can we do that?
11
lazy val
Scala defines the lazy modifier to indicate a value that should be computed lazily
only when accessed.
If we want o do so, the only thing we need to change is the definition of the val,
as shown below:
1 package edu.esiee.scala17.ex3
2
3 import edu.esiee.scala17.ex2.Shape
4
5 class Circle(val radius: Float) extends Shape {
6 def scale(a: Float): Shape = new Circle(a * radius)
7
8 def show(): Unit = print(s"Circle($radius)")
9
10 lazy val area: Float = Math.PI.toFloat*radius*radius
11 lazy val perimeter: Float = 2*Math.PI.toFloat*radius
12 }
12
Overriding
A subclass can override both methods as well as val defined in the parent class.
The super-class constructor can also be explicitly called as part of the extend
declaration as shown in the example below:
1 package edu.esiee.scala17.ex3
2
3 class FastCircle(r: Float) extends Circle(r) {
4 override val perimeter: Float = Math.PI.toFloat * (radius.toInt << 1)
5
6 override def show(): Unit = print(s"O($radius")
7 }
13
Scala Type Hierarchy
14
Algebraic Data Types
Algebraic Data Type
In type theory and commonly in functional programming, an algebraic data
type is a kind of composite type. In other term a type obtained by composing
other types.
Depending on the composition operator we can have product types and sum
types
15
Product Types
Product types are algebraic data types in which the algebra is product
A product type is defined by the conjunction of two or more types, called fields.
The set of all possible values of a product type is the set-theoretic product, i.e.,
the Cartesian product, of the sets of all possible values of its field types.
Example
data Point = Point Double Double
In the example above the product type Point is defined by the Cartesian product
of Int × Int
16
Sum Types
The values of a sum type are typically grouped into several classes, called
variants.
A value of a variant type is usually created with a quasi-functional entity called a
constructor.
Each variant has its own constructor, which takes a specified number of
arguments with specified types.
17
Sum Types Cont.
The set of all possible values of a sum type is the set-theoretic sum, i.e., the
disjoint union, of the sets of all possible values of its variants.
Enumerated types are a special case of sum types in which the constructors take
no arguments, as exactly one value is defined for each constructor.
Values of algebraic types are analyzed with pattern matching, which identifies a
value by its constructor or field names and extracts the data it contains.
Example
data Tree = Empty
| Leaf Int
| Node Tree Tree deriving (Show)
> let t = Node (Leaf 1) (Node (Leaf 2) (Leaf 3))
> :t t
t :: Tree
18
Case Classes
and
Pattern Matching
Case Classes
Scala case classes provide a way to mimic algebraic data types as found in
functional programming languages.
The Scala version of the Tree type we defined in Haskell is:
1 package edu.esiee.scala17.ex4
2
3 abstract class Tree
4
5 object Empty extends Tree
6
7 case class Leaf(v: Int) extends Tree
8
9 case class Node(l: Tree, r: Tree) extends Tree
1 val t = Node(Leaf(1), Node(Leaf(2), Leaf(3)))
19
Case Classes / Cont.
If you noticed the Tree algebraic data type was declared through case classes
that did not have named attributes. In other term, there were no val or var.
That does not mean that you cannot declare val or var as part of a case class. It
has more to do with the fact that case classes are worked upon using pattern
matching.
20
Pattern Matching
Scala pattern matching has the following structure:
selector match alternatives
A pattern match includes a series of alternatives, each starting with the keyword
case.
Each alternative includes a pattern and one or more expressions, which are
evaluated only if the pattern matches.
Let’s see what are the kinds of patterns supported by Scala.
21
Wildcard Pattern
The wildcard pattern, denotated by , matches anything – thus its appelation.
Example
1 def isEmpty(xs: List[Int]) =
2 xs match {
3 case List() => return true
4 case _ => return false
5 }
6
7 def isSingleElementList(xs: List[Int]) =
8 xs match {
9 case List(_) => true
10 case _ => false
11 }
22
Constant Pattern
A constant pattern matches only itself. Any literal, such as 18, 42, "Ciao",
true can be used as a constant.
Any val or singleton object can also be used as a constant.
Example
1 def toString(d: Int) =
2 d match {
3 case 0 => "Zero"
4 case 1 => "Uno"
5 // ...
6 case 9 => "Nove"
7 }
23
Variable Pattern
A variable pattern matches any object, just like a wildcard. But unlike a wildcard,
Scala binds the variable to whatever the object is.
You can then use this variable to act on the object further.
Example
1 def checkZero(d: Int) =
2 d match {
3 0 => "zero"
4 v => v + " is not zero"
5 }
24
Constructor Pattern
A constructor pattern looks like Node(Leaf(1), Node(Leaf(2), )) . It
consists of a name (Tree) and then a number of patterns within parentheses.
Assuming the name designates a case class, such a pattern means to first check
that the object is a member of the named case class, and then to check that the
constructor parameters of the object match the extra patterns supplied.
Example
1 def triangleTree(t: Tree) =
2 t match {
3 case Node(Leaf(_), Leaf(_)) => True
4 case _ => False
5 }
Please notice that Sequence and Tuple patters are just a special case of
constructor patterns.
25
Exercise
Define the following functions for our Tree type:
1. height which computes the tree height.
2. sum which is the function that computes the sum of all the elements of the
tree.
3. fold which is a function that applies a generic binary operator to the tree,
where the zero is used when for empty nodes.
26
Typed Pattern
Typed pattern are used to test and cast types.
Example
1 def generalSize(x: Any) = x match {
2 case s: String => s.length
3 case m: Map[_, _] => m.size
4 case _ => -1
5 }
27
Pattern Guards
A pattern guard comes after a pattern and starts with an if. The guard can be
an arbitrary boolean expression, which typically refers to variables in the pattern.
If a pattern guard is present, the match succeeds only if the guard evaluates to
true.
Example
1 // match only positive integers
2 case n: Int if 0 < n => ...
3
4 // match only strings starting with the letter `a'
5 case s: String if s(0) == 'a' => ...
28
Other use of Patterns
Patterns can be used also in assignments, such as in:
1 val (a, b, c) = tripetFun(something)
Patterns can also be used in for expressions:
1 for ((key, value) <- store)
2 print("The key = " + key + "has value = " + value)
29
For Expression Revisited
For Expression
The general form of a for expression is:
for ( seq ) yield expr
Here, seq is a sequence of generators, definitions, and filters, with semicolons
between successive elements.
30
Generator, Definition and Filter
A generator is of the form:
pat ← expr
A definition is of the form:
pat = expr
A filter is of the form:
if expr
31
for expression: Examples
1 for (x <- List(1, 2); y <- List("one", "two")) yield (x, y)
2
3 for ( i <- 1 to 10; j <- i to 10) yield (i,j)
4
5 val names = List("Morpheus", "Neo", "Trinity", "Tank", "Dozer")
6
7 for (name <- names; if name.length == 3) yield name
8
9 val xxs = List(List(1,2,3,4), List(6, 8, 10, 12), List(14, 16, 18))
10
11 for (xs <- xxs; e <- xs; if (e % 2 == 0)) yield e
12
13 for (xs <- xxs; if (xs.isEmpty == false); h = xs.head) yield h
32
Type Parameterization
Type Constructors in Scala
Beside first order types, Scala supports Type Constructors, – a kind of higher
order types.
In the case of Scala, a parametric class is an n-ary type operator taking as
argument one or more types, and returning another type.
Example
The list time we have seen this far, is a type constructor declared as:
classList[+T]
Thus List[Int] and List[String] are two instances of the List[+T] type constructor.
33
Ad

More Related Content

What's hot (20)

Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
HalaiHansaika
 
Functional object
Functional objectFunctional object
Functional object
ruchijindal87
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
vsssuresh
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scala
Amuhinda Hungai
 
Templates
TemplatesTemplates
Templates
Pranali Chaudhari
 
Knolx session
Knolx sessionKnolx session
Knolx session
Knoldus Inc.
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
C# String
C# StringC# String
C# String
Raghuveer Guthikonda
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
DataminingTools Inc
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
Gautham Rajesh
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
DataminingTools Inc
 
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Hoang Nguyen
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
Neeraj Kaushik
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
HalaiHansaika
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
vsssuresh
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scala
Amuhinda Hungai
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
Gautham Rajesh
 
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Hoang Nguyen
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 

Similar to Programming in Scala - Lecture Three (20)

Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
ehsoon
 
scala.ppt
scala.pptscala.ppt
scala.ppt
Harissh16
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
University of Salerno
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
Meetu Maltiar
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
University of Salerno
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
Knoldus Inc.
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
Kirill Kozlov
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
Kirill Kozlov
 
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptxM251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
smartashammari
 
Scala google
Scala google Scala google
Scala google
Navneet Kumar
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
Nagavarunkumar Kolla
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
Tim (dev-tim) Zadorozhniy
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
WIDI ediot autis dongok part 1.ediot lu lemot lu setan luWIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
IrlanMalik
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
Ayush Mishra
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Muhammad Fayyaz
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
ehsoon
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
Kirill Kozlov
 
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptxM251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
smartashammari
 
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
WIDI ediot autis dongok part 1.ediot lu lemot lu setan luWIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
IrlanMalik
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
Ayush Mishra
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Ad

More from Angelo Corsaro (20)

Zenoh: The Genesis
Zenoh: The GenesisZenoh: The Genesis
Zenoh: The Genesis
Angelo Corsaro
 
zenoh: The Edge Data Fabric
zenoh: The Edge Data Fabriczenoh: The Edge Data Fabric
zenoh: The Edge Data Fabric
Angelo Corsaro
 
Zenoh Tutorial
Zenoh TutorialZenoh Tutorial
Zenoh Tutorial
Angelo Corsaro
 
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair MonetisationData Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
Angelo Corsaro
 
zenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query computezenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query compute
Angelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
Angelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
Angelo Corsaro
 
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog ComputingBreaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Angelo Corsaro
 
Eastern Sicily
Eastern SicilyEastern Sicily
Eastern Sicily
Angelo Corsaro
 
fog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructurefog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructure
Angelo Corsaro
 
Cyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT AgeCyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT Age
Angelo Corsaro
 
fog05: The Fog Computing Platform
fog05: The Fog Computing Platformfog05: The Fog Computing Platform
fog05: The Fog Computing Platform
Angelo Corsaro
 
Data Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained EnvionrmentsData Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained Envionrments
Angelo Corsaro
 
The DDS Security Standard
The DDS Security StandardThe DDS Security Standard
The DDS Security Standard
Angelo Corsaro
 
The Data Distribution Service
The Data Distribution ServiceThe Data Distribution Service
The Data Distribution Service
Angelo Corsaro
 
RUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsRUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming Ruminations
Angelo Corsaro
 
Vortex II -- The Industrial IoT Connectivity Standard
Vortex II -- The  Industrial IoT  Connectivity StandardVortex II -- The  Industrial IoT  Connectivity Standard
Vortex II -- The Industrial IoT Connectivity Standard
Angelo Corsaro
 
Fog Computing Defined
Fog Computing DefinedFog Computing Defined
Fog Computing Defined
Angelo Corsaro
 
DDS In Action Part II
DDS In Action Part IIDDS In Action Part II
DDS In Action Part II
Angelo Corsaro
 
DDS in Action -- Part I
DDS in Action -- Part IDDS in Action -- Part I
DDS in Action -- Part I
Angelo Corsaro
 
zenoh: The Edge Data Fabric
zenoh: The Edge Data Fabriczenoh: The Edge Data Fabric
zenoh: The Edge Data Fabric
Angelo Corsaro
 
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair MonetisationData Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
Angelo Corsaro
 
zenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query computezenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query compute
Angelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
Angelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
Angelo Corsaro
 
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog ComputingBreaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Angelo Corsaro
 
fog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructurefog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructure
Angelo Corsaro
 
Cyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT AgeCyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT Age
Angelo Corsaro
 
fog05: The Fog Computing Platform
fog05: The Fog Computing Platformfog05: The Fog Computing Platform
fog05: The Fog Computing Platform
Angelo Corsaro
 
Data Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained EnvionrmentsData Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained Envionrments
Angelo Corsaro
 
The DDS Security Standard
The DDS Security StandardThe DDS Security Standard
The DDS Security Standard
Angelo Corsaro
 
The Data Distribution Service
The Data Distribution ServiceThe Data Distribution Service
The Data Distribution Service
Angelo Corsaro
 
RUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsRUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming Ruminations
Angelo Corsaro
 
Vortex II -- The Industrial IoT Connectivity Standard
Vortex II -- The  Industrial IoT  Connectivity StandardVortex II -- The  Industrial IoT  Connectivity Standard
Vortex II -- The Industrial IoT Connectivity Standard
Angelo Corsaro
 
DDS in Action -- Part I
DDS in Action -- Part IDDS in Action -- Part I
DDS in Action -- Part I
Angelo Corsaro
 
Ad

Recently uploaded (20)

fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 

Programming in Scala - Lecture Three

  • 1. Programming in Scala Lecture Three Angelo Corsaro 13 December 2017 Chief Technology Officer, ADLINK Technology Inc.
  • 2. Table of contents 1. Reviewing Fold 2. More on Scala Classes 3. Algebraic Data Types 4. Case Classes and Pattern Matching 5. For Expression Revisited 6. Type Parameterization 1
  • 4. foldr and foldl 1 def reverse(xs: List[Int]): List[Int] = xs match { 2 case List() => List() 3 case y::ys => reverse(ys) ::: List(y) 4 } 5 6 7 val xs = List(1,2,3,4,5) 8 val ys = reverse(xs) 9 10 val zs = ys.foldLeft(List[Int]())((xs: List[Int], x: Int) => x :: xs) 11 12 val ks = zs.foldRight(List[Int]())((x: Int, xs: List[Int]) => xs ::: List(x) ) 2
  • 6. More on Scala Classes
  • 7. Scala Classes Refresher In the first lecture we saw the basic mechanism provided by Scala to declare Classes. Back than we had seen closures without realizing it, thus let’s take a fresh look at some of the examples: 1 class Complex(val re: Double, val im: Double) { 2 override def toString = s"$re+i$im" 3 } 4 5 class Rational(a: Int, b: Int) { 6 assert(b != 0) 7 val num: Int = a / gcd(a, b) 8 val den: Int = b / gcd(a, b) 9 10 def this(n: Int) = this(n, 1) 11 } 4
  • 8. Abstract Classes In Scala the abstract modifier is used to denote an abstract class. In other terms, a class that cannot be instantiated. An abstract class may have abstract members that don’t have an implementation (definition). 1 package edu.esiee.scala17.ex1 2 3 abstract class Shape { 4 def scale(a: Float): Shape 5 6 // Parameter-less methods: Two takes: 7 def show(): Unit 8 9 def area: Float 10 def perimeter: Float 11 12 } 5
  • 9. Parameter-less Methods Parameter-less methods can be declared with or without parenthesis. The convention is that parenthesis are used for methods that cause side-effects – like show above. Parenthesis are not used to methods that compute properties. This way there uniform access between attributes and methods. 6
  • 10. Extending a Class The Shape abstract type had defined some of the characteristics shared by some geometrical shapes. A concrete shape can be defined by extending the abstract Shape class as follows: 1 package edu.esiee.scala17.ex1 2 3 class Circle(val radius: Float) extends Shape { 4 5 def scale(a: Float): Shape = new Circle(a * radius) 6 7 def show(): Unit = print(s"Circle($radius)") 8 9 def area: Float = Math.PI.toFloat*radius*radius 10 def perimeter: Float = 2*Math.PI.toFloat*radius 11 } 7
  • 11. Methods or Vals? The area and perimeter have been defined as parenthesis less methods. As our shape are immutable, a valid question is if we could define them with vals and compute them only once, as opposed to every-time the method is called. The good news is that Scala supports the concept of abstract val, let’s see how to leverage them. 8
  • 12. Abstract val Along with abstract methods, an abstract class can also have abstract vals. These are values which are declared but not defined. If we were to leverage abstract values our Shape class could be declared as follows: 1 package edu.esiee.scala17.ex2 2 3 abstract class Shape { 4 def scale(a: Float): Shape 5 6 def show(): Unit 7 8 val area: Float 9 val perimeter: Float 10 11 } 9
  • 13. Implementing abstract val Implementing an abstract val is as simple ad defining it as show in the code example below: 1 package edu.esiee.scala17.ex2 2 3 class Circle(val radius: Float) extends Shape { 4 5 def scale(a: Float): Shape = new Circle(a * radius) 6 7 def show(): Unit = print(s"Circle($radius)") 8 9 val area: Float = Math.PI.toFloat*radius*radius 10 val perimeter: Float = 2*Math.PI.toFloat*radius 11 } 10
  • 14. Observation Now, as we are using val the shape area is computed only once, at the time of initialization. This is an improvement compared from the previous example. Additionally, the client code cannot tell whether we are implementing the area and the perimeter with a val or with a parenthesis less method which is very good. Yet... We are still spending the time even if the client code is never calling that operation. In an ideal world we would want to compute it once and only if needed... Can we do that? 11
  • 15. lazy val Scala defines the lazy modifier to indicate a value that should be computed lazily only when accessed. If we want o do so, the only thing we need to change is the definition of the val, as shown below: 1 package edu.esiee.scala17.ex3 2 3 import edu.esiee.scala17.ex2.Shape 4 5 class Circle(val radius: Float) extends Shape { 6 def scale(a: Float): Shape = new Circle(a * radius) 7 8 def show(): Unit = print(s"Circle($radius)") 9 10 lazy val area: Float = Math.PI.toFloat*radius*radius 11 lazy val perimeter: Float = 2*Math.PI.toFloat*radius 12 } 12
  • 16. Overriding A subclass can override both methods as well as val defined in the parent class. The super-class constructor can also be explicitly called as part of the extend declaration as shown in the example below: 1 package edu.esiee.scala17.ex3 2 3 class FastCircle(r: Float) extends Circle(r) { 4 override val perimeter: Float = Math.PI.toFloat * (radius.toInt << 1) 5 6 override def show(): Unit = print(s"O($radius") 7 } 13
  • 19. Algebraic Data Type In type theory and commonly in functional programming, an algebraic data type is a kind of composite type. In other term a type obtained by composing other types. Depending on the composition operator we can have product types and sum types 15
  • 20. Product Types Product types are algebraic data types in which the algebra is product A product type is defined by the conjunction of two or more types, called fields. The set of all possible values of a product type is the set-theoretic product, i.e., the Cartesian product, of the sets of all possible values of its field types. Example data Point = Point Double Double In the example above the product type Point is defined by the Cartesian product of Int × Int 16
  • 21. Sum Types The values of a sum type are typically grouped into several classes, called variants. A value of a variant type is usually created with a quasi-functional entity called a constructor. Each variant has its own constructor, which takes a specified number of arguments with specified types. 17
  • 22. Sum Types Cont. The set of all possible values of a sum type is the set-theoretic sum, i.e., the disjoint union, of the sets of all possible values of its variants. Enumerated types are a special case of sum types in which the constructors take no arguments, as exactly one value is defined for each constructor. Values of algebraic types are analyzed with pattern matching, which identifies a value by its constructor or field names and extracts the data it contains. Example data Tree = Empty | Leaf Int | Node Tree Tree deriving (Show) > let t = Node (Leaf 1) (Node (Leaf 2) (Leaf 3)) > :t t t :: Tree 18
  • 24. Case Classes Scala case classes provide a way to mimic algebraic data types as found in functional programming languages. The Scala version of the Tree type we defined in Haskell is: 1 package edu.esiee.scala17.ex4 2 3 abstract class Tree 4 5 object Empty extends Tree 6 7 case class Leaf(v: Int) extends Tree 8 9 case class Node(l: Tree, r: Tree) extends Tree 1 val t = Node(Leaf(1), Node(Leaf(2), Leaf(3))) 19
  • 25. Case Classes / Cont. If you noticed the Tree algebraic data type was declared through case classes that did not have named attributes. In other term, there were no val or var. That does not mean that you cannot declare val or var as part of a case class. It has more to do with the fact that case classes are worked upon using pattern matching. 20
  • 26. Pattern Matching Scala pattern matching has the following structure: selector match alternatives A pattern match includes a series of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which are evaluated only if the pattern matches. Let’s see what are the kinds of patterns supported by Scala. 21
  • 27. Wildcard Pattern The wildcard pattern, denotated by , matches anything – thus its appelation. Example 1 def isEmpty(xs: List[Int]) = 2 xs match { 3 case List() => return true 4 case _ => return false 5 } 6 7 def isSingleElementList(xs: List[Int]) = 8 xs match { 9 case List(_) => true 10 case _ => false 11 } 22
  • 28. Constant Pattern A constant pattern matches only itself. Any literal, such as 18, 42, "Ciao", true can be used as a constant. Any val or singleton object can also be used as a constant. Example 1 def toString(d: Int) = 2 d match { 3 case 0 => "Zero" 4 case 1 => "Uno" 5 // ... 6 case 9 => "Nove" 7 } 23
  • 29. Variable Pattern A variable pattern matches any object, just like a wildcard. But unlike a wildcard, Scala binds the variable to whatever the object is. You can then use this variable to act on the object further. Example 1 def checkZero(d: Int) = 2 d match { 3 0 => "zero" 4 v => v + " is not zero" 5 } 24
  • 30. Constructor Pattern A constructor pattern looks like Node(Leaf(1), Node(Leaf(2), )) . It consists of a name (Tree) and then a number of patterns within parentheses. Assuming the name designates a case class, such a pattern means to first check that the object is a member of the named case class, and then to check that the constructor parameters of the object match the extra patterns supplied. Example 1 def triangleTree(t: Tree) = 2 t match { 3 case Node(Leaf(_), Leaf(_)) => True 4 case _ => False 5 } Please notice that Sequence and Tuple patters are just a special case of constructor patterns. 25
  • 31. Exercise Define the following functions for our Tree type: 1. height which computes the tree height. 2. sum which is the function that computes the sum of all the elements of the tree. 3. fold which is a function that applies a generic binary operator to the tree, where the zero is used when for empty nodes. 26
  • 32. Typed Pattern Typed pattern are used to test and cast types. Example 1 def generalSize(x: Any) = x match { 2 case s: String => s.length 3 case m: Map[_, _] => m.size 4 case _ => -1 5 } 27
  • 33. Pattern Guards A pattern guard comes after a pattern and starts with an if. The guard can be an arbitrary boolean expression, which typically refers to variables in the pattern. If a pattern guard is present, the match succeeds only if the guard evaluates to true. Example 1 // match only positive integers 2 case n: Int if 0 < n => ... 3 4 // match only strings starting with the letter `a' 5 case s: String if s(0) == 'a' => ... 28
  • 34. Other use of Patterns Patterns can be used also in assignments, such as in: 1 val (a, b, c) = tripetFun(something) Patterns can also be used in for expressions: 1 for ((key, value) <- store) 2 print("The key = " + key + "has value = " + value) 29
  • 36. For Expression The general form of a for expression is: for ( seq ) yield expr Here, seq is a sequence of generators, definitions, and filters, with semicolons between successive elements. 30
  • 37. Generator, Definition and Filter A generator is of the form: pat ← expr A definition is of the form: pat = expr A filter is of the form: if expr 31
  • 38. for expression: Examples 1 for (x <- List(1, 2); y <- List("one", "two")) yield (x, y) 2 3 for ( i <- 1 to 10; j <- i to 10) yield (i,j) 4 5 val names = List("Morpheus", "Neo", "Trinity", "Tank", "Dozer") 6 7 for (name <- names; if name.length == 3) yield name 8 9 val xxs = List(List(1,2,3,4), List(6, 8, 10, 12), List(14, 16, 18)) 10 11 for (xs <- xxs; e <- xs; if (e % 2 == 0)) yield e 12 13 for (xs <- xxs; if (xs.isEmpty == false); h = xs.head) yield h 32
  • 40. Type Constructors in Scala Beside first order types, Scala supports Type Constructors, – a kind of higher order types. In the case of Scala, a parametric class is an n-ary type operator taking as argument one or more types, and returning another type. Example The list time we have seen this far, is a type constructor declared as: classList[+T] Thus List[Int] and List[String] are two instances of the List[+T] type constructor. 33
  翻译: