SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Programming in Scala
Lecture Four
Angelo Corsaro
15 December 2017
Chief Technology Officer, ADLINK Technology Inc.
Table of contents
1. Implicits
2. Monads: A Gentle Introduction
3. Parallel and Concurrent Computations
1
Implicits
Scala Implicits
Scala implicit definitions provide a general mechanism to control how the
compiler resolves types or argument mismatches.
In other terms, it control the set of transformation available to the compiler to
fix a compile time error.
2
Example
Suppose we have a function that sums the elements of a lists and we want to
also use it to sum arrays.
If we just pass an array the compiler will raise a type error as he does not know
how to convert an Array[Int] into a List[Int].
def sum(xs: List[Int]): Int = xs match {
case List() => 0
case y::ys => y + sum(ys)
}
sum(Array[Int](1,3,4,5,6)) // Compiler error!
3
Implicit Functions
To fix this error and allow this function operate on Arrays as well as on List we
can define an implicit conversion as shown below:
import scala.language.implicitConversions
object Converter {
implicit def array2List(as: Array[Int])= as.toList
implicit def strintToInt(s: String) = s.toInt
}
import Converter._
sum(Array(1,3,4,5,6)) // Compiler inserts the conversion array2List
sum(Array[Int](1,"3",4,"5",6)) // Compiler inserts the conversions array2List and stringToInt
4
Implicit Classes
Implicit classes are commonly used to emulate open classes in Scala.
In other terms, one can define implicit transformation to extend or enrich the
protocol supported bu given type.
Example
object Implicits {
implicit class RichRunnable[T](runner: => T) extends Runnable {
def run() { runner() }
}
}
val thread = new Thread {
print("Hello!")
}
5
Implicit Parameters
Implicit parameters can be used to let the compiler fill in missing parameters in
curried functions.
Example
object Logger {
def log(msg: String)(implicit prompt: String) {
println(prompt + msg)
}
}
implicit val defaultPrompt = ">>> "
Logger.log("Testing logger")
6
Implicit Resolution Rules
The compiler looks for potential conversions available in the current scope.
Thus, conversions needs to be imported in order for the compiler to apply them.
7
Monads: A Gentle Introduction
Monad: Basic Idea
Monads can be thought of as composable computation descriptions.
The essence of monad is the separation of composition timeline from the
composed computation’s execution timeline, as well as the ability of computation
to implicitly carry extra data, as pertaining to the computation itself, in addition
to its one (hence the name) output, that it will produce when run (or queried, or
called upon).
This lends monads to supplementing pure calculations with features like I/O,
common environment, updatable state, etc.
8
Monad: Basic Idea / Cont.
Each monad, or computation type, provides means, subject to Monad Laws, to:
• create a description of a computation that will produce (a.k.a. ”return”) a
value, and
• combine (a.k.a. ”bind”) a computation description with a reaction to it, a
pure function that is set to receive a computation-produced value (when and
if that happens) and return another computation description.
Reactions are thus computation description constructors. A monad might also
define additional primitives to provide access to and/or enable manipulation of
data it implicitly carries, specific to its nature; cause some specific side-effects;
etc..
9
Monad Class and Monad Laws
Monads can be viewed as a standard programming interface to various data or
control structures, which is captured by the Monad class. All common monads
are members of it:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
In addition to implementing the class functions, all instances of Monad should
obey the following equations, or Monad Laws:
return a >>= k = k a
m >>= return = m
m >>= (x -> k x >>= h) = (m >>= k) >>= h
10
Maybe Monad
The Haskell Maybe type is defined as:
data Maybe a = Just a | Nothing
deriving (Eq, Ord)
It is an instance of the Monad and Functor classes.
11
Maybe Monad Implementation
Let’s see how the Monad operations are defined for the Maybe type:
return :: a -> Maybe a
return x = Just x
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) Nothing _ = Nothing
(>>=) (Just x) f = f x
(>>) :: Maybe a -> Maybe b -> Maybe b
(>>) x y = x >>= (_ -> y)
12
Verifying the First Monad Laws for Maybe
The first law states that:
return a >>= k = k a
Let’s play the substitution game:
return a >>= Just a >>= k = k a
13
Verifying the Second Monad Laws for Maybe
The second law states that:
m >>= return = m
Let’s play the substitution game:
Just x >>= return = return x = Just x
Nothing >>= return = Nothing
14
Verifying the Third Monad Laws for Maybe
The third law states that:
m >>= (x -> k x >>= h) = (m >>= k) >>= h
Let’s play the substitution game:
Just x >>= (y -> k y >>= h) = Just k x >>= h = (Just k x) >>= h = (Just x >>= k) >>= h
Thus we have proved that the Maybe type satisfies the three key monads rule.
There are types you are already familiar with that satisfy the monadic laws, such
as lists.
15
Monads in Scala
Scala does not define a Monad higher-order type, but monadic type (as well as
some time that are not strictly monads), provide the key monadic operations.
These operations, however are named differently.
16
Option Type in Scala
The Option type is Scala’s equivalent to Haskell’s Maybe Monad. Looking at it
is a good way of learning about monadic computations in Scala.
The Option type is defined as follows:
sealed abstract class Option[+A] extends Product with Serializable
case object None extends Option[Nothing] {
// ...
}
final case class Some[+A](value: A) extends Option[A] {
// ...
}
17
Option Type in Scala
The monad operations provided are the following:
// this is the equivalent of Haskell's bind (>>=)
def flatMap[B](f: A => Option[B]): Option[B]
Notice that the equivalent of return is the constructor. That said, Scala does not
provide the sequencing operator (>>). But as we’ve seen that can be easily
defined from bind.
The Option type is technically also a functor (as lists also are), thus it also
provides the map operation.
// this is the equivalent of Haskell's bind (>>=)
def map[B](f: A => B): B
18
Monadic Computation with the Option Type
def parseInt(s: String): Option[Int] = {
try {
Some(s.toInt)
} catch {
case e: java.lang.NumberFormatException => None
}
}
val x = parseInt("10")
val y = parseInt("10")
val z = x.flatMap(a => y.flatMap(b => Some(a+b)))
// Or using for
for (a <- x; b <- y) yield (a + b)
19
Parallel and Concurrent
Computations
Motivation
As the number of cores on processors are constantly increasing while the clock
frequencies are essentially stagnating, exploiting the additional computational
power requires to leverage parallel and concurrent computations.
Scala provides some elegant mechanism for supporting both parallel as well as
concurrent computations.
20
Parallel Collections
Scala supports the following parallel collections:
• ParArray
• ParVector
• mutable.ParHashMap
• mutable.ParHashSet
• immutable.ParHashMap
• immutable.ParHashSet
• ParRange
• ParTrieMap
21
Using Parallel Collections
Using parallel collections is rather trivial. A parallel collection can be created
from a regular one by using the .par property.
Example
val parArray = (1 to 10000).toArray.par
parArray.fold(0)(_ + _)
val lastNames = List("Smith","Jones","Frankenstein","Bach","Jackson","Rodin").par
lastNames.map(_.toUpperCase)
Given this out-of-order semantics, also must be careful to perform only
associative operations in order to avoid non-determinism.
22
Concurrent Computation on the JVM
The traditional approach to concurrent computations on the JVM is to leverage
threads and the built-in support for monitors, through the synchronized keyword,
to avoid race conditions.
This concurrency model is based on the assumption that different threads of
execution collaborate by mutating some shared state. This shared state has to be
protected to avoid race conditions.
This programming model is quite error-prone and is often source of bugs that are
very hard to debug due to the inherent non-determinism of concurrent
computations.
Furthermore, lock-based synchronization can lead to deadlocks.
23
Scala futures
Scala provide a mechanism for concurrent execution that makes it easier to work
under the share nothing model and that support more importantly supports
composability.
This mechanism is provided by Scala’s futures.
Futures are monads, thus, you can operate and compose them as any other
monad.
24
Futures Example
Below is an example of computing and composing futures.
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
val f1 = Future {
Thread.sleep(2)
10
}
val f2 = Future {
Thread.sleep(1)
20
}
val c = for (a <- f1; b <- f2) yield a + b
c.onComplete(r => r.foreach(a => println(a)))
25
Promises
The most general way of creating a future and is through promises.
The completion of future create from a promise is controlled by completing the
promise.
Example
def asynchComputation(): Future[Int]
import Converters._
val promise = Promise[Int]
new Thread {
val r = someComputeIntenseFun()
promise.success(r)
}.start()
return promise.future
}
26
Homeworks
Reading Assignment
Read the following chapters:
• Chapter 21
• Chapter 32
27

Más contenido relacionado

La actualidad más candente

Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glanceKnoldus Inc.
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...Philip Schwarz
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: NotesRoberto Casadei
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++HalaiHansaika
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingShine Xavier
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scalaAmuhinda Hungai
 
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)Wim Vanderbauwhede
 
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 IIIAjit Nayak
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Philip Schwarz
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programmingnmahi96
 

La actualidad más candente (20)

Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scala
 
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)
Perl and Haskell: Can the Twain Ever Meet? (tl;dr: yes)
 
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
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
 
Haskell
HaskellHaskell
Haskell
 
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Templates
TemplatesTemplates
Templates
 
C# String
C# StringC# String
C# String
 

Similar a Programming in Scala - Lecture Four

Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesWim Vanderbauwhede
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 

Similar a Programming in Scala - Lecture Four (20)

Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Oct.22nd.Presentation.Final
Oct.22nd.Presentation.FinalOct.22nd.Presentation.Final
Oct.22nd.Presentation.Final
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
Practical cats
Practical catsPractical cats
Practical cats
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic Languages
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 

Más de Angelo Corsaro

zenoh: The Edge Data Fabric
zenoh: The Edge Data Fabriczenoh: The Edge Data Fabric
zenoh: The Edge Data FabricAngelo 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 MonetisationAngelo 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 computeAngelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolAngelo Corsaro
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolAngelo 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 ComputingAngelo Corsaro
 
fog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructurefog05: The Fog Computing Infrastructure
fog05: The Fog Computing InfrastructureAngelo 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 AgeAngelo Corsaro
 
fog05: The Fog Computing Platform
fog05: The Fog Computing Platformfog05: The Fog Computing Platform
fog05: The Fog Computing PlatformAngelo 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 EnvionrmentsAngelo Corsaro
 
The DDS Security Standard
The DDS Security StandardThe DDS Security Standard
The DDS Security StandardAngelo Corsaro
 
The Data Distribution Service
The Data Distribution ServiceThe Data Distribution Service
The Data Distribution ServiceAngelo Corsaro
 
RUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsRUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsAngelo 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 StandardAngelo Corsaro
 
DDS in Action -- Part I
DDS in Action -- Part IDDS in Action -- Part I
DDS in Action -- Part IAngelo Corsaro
 

Más de Angelo Corsaro (20)

Zenoh: The Genesis
Zenoh: The GenesisZenoh: The Genesis
Zenoh: The Genesis
 
zenoh: The Edge Data Fabric
zenoh: The Edge Data Fabriczenoh: The Edge Data Fabric
zenoh: The Edge Data Fabric
 
Zenoh Tutorial
Zenoh TutorialZenoh Tutorial
Zenoh Tutorial
 
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
 
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
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
 
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
 
Eastern Sicily
Eastern SicilyEastern Sicily
Eastern Sicily
 
fog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructurefog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructure
 
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
 
fog05: The Fog Computing Platform
fog05: The Fog Computing Platformfog05: The Fog Computing Platform
fog05: The Fog Computing Platform
 
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
 
The DDS Security Standard
The DDS Security StandardThe DDS Security Standard
The DDS Security Standard
 
The Data Distribution Service
The Data Distribution ServiceThe Data Distribution Service
The Data Distribution Service
 
RUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsRUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming Ruminations
 
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
 
Fog Computing Defined
Fog Computing DefinedFog Computing Defined
Fog Computing Defined
 
DDS In Action Part II
DDS In Action Part IIDDS In Action Part II
DDS In Action Part II
 
DDS in Action -- Part I
DDS in Action -- Part IDDS in Action -- Part I
DDS in Action -- Part I
 

Último

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Último (20)

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

Programming in Scala - Lecture Four

  • 1. Programming in Scala Lecture Four Angelo Corsaro 15 December 2017 Chief Technology Officer, ADLINK Technology Inc.
  • 2. Table of contents 1. Implicits 2. Monads: A Gentle Introduction 3. Parallel and Concurrent Computations 1
  • 4. Scala Implicits Scala implicit definitions provide a general mechanism to control how the compiler resolves types or argument mismatches. In other terms, it control the set of transformation available to the compiler to fix a compile time error. 2
  • 5. Example Suppose we have a function that sums the elements of a lists and we want to also use it to sum arrays. If we just pass an array the compiler will raise a type error as he does not know how to convert an Array[Int] into a List[Int]. def sum(xs: List[Int]): Int = xs match { case List() => 0 case y::ys => y + sum(ys) } sum(Array[Int](1,3,4,5,6)) // Compiler error! 3
  • 6. Implicit Functions To fix this error and allow this function operate on Arrays as well as on List we can define an implicit conversion as shown below: import scala.language.implicitConversions object Converter { implicit def array2List(as: Array[Int])= as.toList implicit def strintToInt(s: String) = s.toInt } import Converter._ sum(Array(1,3,4,5,6)) // Compiler inserts the conversion array2List sum(Array[Int](1,"3",4,"5",6)) // Compiler inserts the conversions array2List and stringToInt 4
  • 7. Implicit Classes Implicit classes are commonly used to emulate open classes in Scala. In other terms, one can define implicit transformation to extend or enrich the protocol supported bu given type. Example object Implicits { implicit class RichRunnable[T](runner: => T) extends Runnable { def run() { runner() } } } val thread = new Thread { print("Hello!") } 5
  • 8. Implicit Parameters Implicit parameters can be used to let the compiler fill in missing parameters in curried functions. Example object Logger { def log(msg: String)(implicit prompt: String) { println(prompt + msg) } } implicit val defaultPrompt = ">>> " Logger.log("Testing logger") 6
  • 9. Implicit Resolution Rules The compiler looks for potential conversions available in the current scope. Thus, conversions needs to be imported in order for the compiler to apply them. 7
  • 10. Monads: A Gentle Introduction
  • 11. Monad: Basic Idea Monads can be thought of as composable computation descriptions. The essence of monad is the separation of composition timeline from the composed computation’s execution timeline, as well as the ability of computation to implicitly carry extra data, as pertaining to the computation itself, in addition to its one (hence the name) output, that it will produce when run (or queried, or called upon). This lends monads to supplementing pure calculations with features like I/O, common environment, updatable state, etc. 8
  • 12. Monad: Basic Idea / Cont. Each monad, or computation type, provides means, subject to Monad Laws, to: • create a description of a computation that will produce (a.k.a. ”return”) a value, and • combine (a.k.a. ”bind”) a computation description with a reaction to it, a pure function that is set to receive a computation-produced value (when and if that happens) and return another computation description. Reactions are thus computation description constructors. A monad might also define additional primitives to provide access to and/or enable manipulation of data it implicitly carries, specific to its nature; cause some specific side-effects; etc.. 9
  • 13. Monad Class and Monad Laws Monads can be viewed as a standard programming interface to various data or control structures, which is captured by the Monad class. All common monads are members of it: class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws: return a >>= k = k a m >>= return = m m >>= (x -> k x >>= h) = (m >>= k) >>= h 10
  • 14. Maybe Monad The Haskell Maybe type is defined as: data Maybe a = Just a | Nothing deriving (Eq, Ord) It is an instance of the Monad and Functor classes. 11
  • 15. Maybe Monad Implementation Let’s see how the Monad operations are defined for the Maybe type: return :: a -> Maybe a return x = Just x (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b (>>=) Nothing _ = Nothing (>>=) (Just x) f = f x (>>) :: Maybe a -> Maybe b -> Maybe b (>>) x y = x >>= (_ -> y) 12
  • 16. Verifying the First Monad Laws for Maybe The first law states that: return a >>= k = k a Let’s play the substitution game: return a >>= Just a >>= k = k a 13
  • 17. Verifying the Second Monad Laws for Maybe The second law states that: m >>= return = m Let’s play the substitution game: Just x >>= return = return x = Just x Nothing >>= return = Nothing 14
  • 18. Verifying the Third Monad Laws for Maybe The third law states that: m >>= (x -> k x >>= h) = (m >>= k) >>= h Let’s play the substitution game: Just x >>= (y -> k y >>= h) = Just k x >>= h = (Just k x) >>= h = (Just x >>= k) >>= h Thus we have proved that the Maybe type satisfies the three key monads rule. There are types you are already familiar with that satisfy the monadic laws, such as lists. 15
  • 19. Monads in Scala Scala does not define a Monad higher-order type, but monadic type (as well as some time that are not strictly monads), provide the key monadic operations. These operations, however are named differently. 16
  • 20. Option Type in Scala The Option type is Scala’s equivalent to Haskell’s Maybe Monad. Looking at it is a good way of learning about monadic computations in Scala. The Option type is defined as follows: sealed abstract class Option[+A] extends Product with Serializable case object None extends Option[Nothing] { // ... } final case class Some[+A](value: A) extends Option[A] { // ... } 17
  • 21. Option Type in Scala The monad operations provided are the following: // this is the equivalent of Haskell's bind (>>=) def flatMap[B](f: A => Option[B]): Option[B] Notice that the equivalent of return is the constructor. That said, Scala does not provide the sequencing operator (>>). But as we’ve seen that can be easily defined from bind. The Option type is technically also a functor (as lists also are), thus it also provides the map operation. // this is the equivalent of Haskell's bind (>>=) def map[B](f: A => B): B 18
  • 22. Monadic Computation with the Option Type def parseInt(s: String): Option[Int] = { try { Some(s.toInt) } catch { case e: java.lang.NumberFormatException => None } } val x = parseInt("10") val y = parseInt("10") val z = x.flatMap(a => y.flatMap(b => Some(a+b))) // Or using for for (a <- x; b <- y) yield (a + b) 19
  • 24. Motivation As the number of cores on processors are constantly increasing while the clock frequencies are essentially stagnating, exploiting the additional computational power requires to leverage parallel and concurrent computations. Scala provides some elegant mechanism for supporting both parallel as well as concurrent computations. 20
  • 25. Parallel Collections Scala supports the following parallel collections: • ParArray • ParVector • mutable.ParHashMap • mutable.ParHashSet • immutable.ParHashMap • immutable.ParHashSet • ParRange • ParTrieMap 21
  • 26. Using Parallel Collections Using parallel collections is rather trivial. A parallel collection can be created from a regular one by using the .par property. Example val parArray = (1 to 10000).toArray.par parArray.fold(0)(_ + _) val lastNames = List("Smith","Jones","Frankenstein","Bach","Jackson","Rodin").par lastNames.map(_.toUpperCase) Given this out-of-order semantics, also must be careful to perform only associative operations in order to avoid non-determinism. 22
  • 27. Concurrent Computation on the JVM The traditional approach to concurrent computations on the JVM is to leverage threads and the built-in support for monitors, through the synchronized keyword, to avoid race conditions. This concurrency model is based on the assumption that different threads of execution collaborate by mutating some shared state. This shared state has to be protected to avoid race conditions. This programming model is quite error-prone and is often source of bugs that are very hard to debug due to the inherent non-determinism of concurrent computations. Furthermore, lock-based synchronization can lead to deadlocks. 23
  • 28. Scala futures Scala provide a mechanism for concurrent execution that makes it easier to work under the share nothing model and that support more importantly supports composability. This mechanism is provided by Scala’s futures. Futures are monads, thus, you can operate and compose them as any other monad. 24
  • 29. Futures Example Below is an example of computing and composing futures. import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future val f1 = Future { Thread.sleep(2) 10 } val f2 = Future { Thread.sleep(1) 20 } val c = for (a <- f1; b <- f2) yield a + b c.onComplete(r => r.foreach(a => println(a))) 25
  • 30. Promises The most general way of creating a future and is through promises. The completion of future create from a promise is controlled by completing the promise. Example def asynchComputation(): Future[Int] import Converters._ val promise = Promise[Int] new Thread { val r = someComputeIntenseFun() promise.success(r) }.start() return promise.future } 26
  • 32. Reading Assignment Read the following chapters: • Chapter 21 • Chapter 32 27