SlideShare a Scribd company logo
1 of 75
Download to read offline
1 
Containers and Aggregates, 
Mutators and Isolates 
for Reactive Programming 
Aleksandar Prokopec, Philipp Haller, Martin Odersky
Reactive Collections 
http://reactive-collections.com 
2
Reactive 
3
4
5 
Observables (event streams)
6 
Observables (event streams) 
• declarative 
val log = messages .filter(_.length < 100) .scan(_ + “n” + _)
7 
Observables (event streams) 
• declarative 
val log = messages .filter(_.length < 100) .scan(_ + “n” + _) 
var log = “” def receive = { case s: String => if (s.length < 100) log = log + “n” + s }
8 
Actors 
• encapsulate mutable state
9 
Actors 
• encapsulate mutable state 
var log = “” def receive = { case s: String => if (s.length < 100) log = log + “n” + s }
10 
Reactive collections 
Isolate Reactive Channel 
Actor ? ActorRef 
? 
Observable 
Observable
11 
Reactive values
Reactive[T] 
12
val ticks: Reactive[Long] 
13 
ticks 
1 
1 
2 
2 
3 
3 
4 
4 
60 
60 
61 
61
ticks onEvent { x => log.debug(s”tick no.$x”) } 
14 
1 
2 
3 
4 
60 
61 
tick no.1 
tick no.2 
tick no.3 
tick no.4 
tick no.60 
tick no.61 
...
ticks foreach { x => log.debug(s”tick no.$x”) } 
15 
1 
2 
3 
4 
60 
61
16 
for (x <- ticks) { log.debug(s”tick no.$x”) } 
Single-threaded!
17 
Reactive combinators
for (x <- ticks) yield { 
x / 60 
} 
18
val seconds: Reactive[Long] = for (x <- ticks) yield { x / 60 } 
19
60 
61 
val seconds: Reactive[Long] = for (x <- ticks) yield { x / 60 } 
20 
ticks 
1 
1 
2 
2 
3 
3 
60 
61 
seconds 
0 
0 
0 
1 
1 
ticks 
seconds 
0 
1
val days: Reactive[Long] = seconds.map(_ / 86400) 
21
val days: Reactive[Long] = 
seconds.map(_ / 86400) 
val secondsToday = 
22
val days: Reactive[Long] = seconds.map(_ / 86400) val secondsToday = (seconds zip days) { (s, d) => s – d * 86400 } 
23
val angle = 
secondsInDay.map(angleFunc) 
24
val angle = secondsInDay.map(angleFunc) val light = secondsInDay.map(lightFunc) 
25
26
27 
val rotate = keys 
a ↓ 
shift ↓ 
a ↑ 
shift ↑ 
pgup ↓ 
pgup ↑ 
keys
28 
val rotate = keys.filter(_ == PAGEUP) 
a ↓ 
shift ↓ 
a ↑ 
shift ↑ 
pgup ↓ 
pgup ↑ 
keys 
pgup ↓ 
pgup ↑ 
filter
29 
val rotate = 
keys.filter(_ == PAGEUP) 
.map(_.down) 
a ↓ 
shift ↓ 
a ↑ 
shift ↑ 
pgup ↓ 
pgup ↑ 
keys 
pgup ↓ 
pgup ↑ 
filter 
true 
false 
map
30 
if (rotate()) viewAngle += 1 
true 
false 
map
31 
Signals
32 
trait Signal[T] extends Reactive[T] { def apply(): T }
33 
val rotate = 
keys.filter(_ == PAGEUP) 
.map(_.down) 
.signal(false) 
true 
false 
map 
signal
34 
val rotate: Signal[Boolean] = keys.filter(_ == PAGEUP) .map(_.down) .signal(false) 
true 
false 
map 
signal
35 
val rotate: Signal[Boolean] val ticks: Reactive[Long] 
ticks
36 
val rotate: Signal[Boolean] 
val ticks: Reactive[Long] 
ticks 
rotate
37 
val rotate: Signal[Boolean] 
val ticks: Reactive[Long] 
val viewAngle: Signal[Double] = 
ticks 
rotate 
viewAngle
38 
val rotate: Signal[Boolean] val ticks: Reactive[Long] val viewAngle: Signal[Double] = ticks.scanPast(0.0) 
ticks 
rotate 
viewAngle
39 
val rotate: Signal[Boolean] val ticks: Reactive[Long] val viewAngle: Signal[Double] = ticks.scanPast(0.0) { (a, _) => if (rotate()) a + 1 else a } 
ticks 
rotate 
viewAngle
40
41 
val velocity = 
ticks.scanPast(0.0) { 
(v, _) => 
} 
val viewAngle =
42 
val velocity = ticks.scanPast(0.0) { (v, _) => if (rotate()) v + 1 } val viewAngle =
43 
val velocity = 
ticks.scanPast(0.0) { 
(v, _) => 
if (rotate()) v + 1 
else v – 0.5 } 
val viewAngle =
44 
val velocity = ticks.scanPast(0.0) { (v, _) => if (rotate()) v + 1 else v – 0.5 } val viewAngle = velocity.scanPast(0.0)(_ + _)
45
46 
Reactive mutators
47 
class Matrix { 
def apply(x: Int, y: Int): Double 
def update(x: Int, y: Int, v: Double) 
}
48 
val screenMat: Signal[Matrix] = (projMat zip viewMat)(_ * _) val invScreenMat = screenMat.map(_.inverse)
49 
Reactive[immutable.Matrix[T]]
50 
val screenMat: Signal[Matrix] = (projMat zip viewMat)(_ * _) val invScreenMat = screenMat.map(_.inverse) 
(4*4*8 + 16 + 16)*4*100 = 64 kb/s
51 
val screenMat = Mutable(new Matrix) (projMat, viewMat).mutate(screenMat) { (p, v) => screenMat().assignMul(p, v) } val invScreenMat = Mutable(new Matrix) screenMat.mutate(invScreenMat) { m => invScreenMat().assignInv(m) }
52 
Reactive collections
53
54 
val selected: Reactive[Set[Character]]
55 
val selected: ReactSet[Character]
56 
trait ReactSet[T] extends ReactContainer[T] { def apply(x: T): Boolean }
57 
trait ReactContainer[T] { 
def inserts: Reactive[T] 
def removes: Reactive[T] 
}
58 
A reactive collection is a pair of reactive values
59 
val selected = 
new ReactHashSet[Character]
60
61 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c)))
62 
class ReactContainer[T] { self => def inserts: Reactive[T] def removes: Reactive[T] def map[S](f: T => S) = new ReactContainer[S] { def inserts: Reactive[T] = self.inserts.map(f) def removes: Reactive[T] = self.removes.map(f) } }
63 
val selected = 
new ReactHashSet[Character] 
val decorations = selected 
.map(c => (c, decoFor(c))) 
.to[ReactHashMap]
64 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
65 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
66 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
67 
val selected = 
new ReactHashSet[Character] 
val decorations = selected 
.map(c => (c, decoFor(c))) 
.react.to[ReactHashMap]
68 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
69 
val selected = 
new ReactHashSet[Character] 
val decorations = selected 
.map(c => (c, decoFor(c))) 
.react.to[ReactHashMap]
70 
val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
71 
Isolates
72 
UI isolate 
class UI extends Isolate[UI.Message] { val frames = source.filter(_ == UI.Frame) val exit = source onCase { case UI.Exit => exit() } } 
Source
73 
UI Isolate 
Source 
AI Isolate 
Source 
Channel[AI.Message]  
 Channel[UI.Message]
74 
Reactive collections 
Isolate Reactive Channel 
Actor ? ActorRef 
? Observable Observable
75 
Thank you!

More Related Content

What's hot

The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196Mahmoud Samir Fayed
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceAlexey Raga
 
Test s velocity_15_5_4
Test s velocity_15_5_4Test s velocity_15_5_4
Test s velocity_15_5_4Kunihiko Saito
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196Mahmoud Samir Fayed
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Leonardo Borges
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignmentashikul akash
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84Mahmoud Samir Fayed
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idrisConor Farrell
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!mickey24
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game ProgrammingRichard Jones
 
SOLID principles in practice: the Clean Architecture
SOLID principles in practice: the Clean ArchitectureSOLID principles in practice: the Clean Architecture
SOLID principles in practice: the Clean ArchitectureFabio Collini
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingSergey Shishkin
 
Effector: we need to go deeper
Effector: we need to go deeperEffector: we need to go deeper
Effector: we need to go deeperVictor Didenko
 

What's hot (20)

The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritance
 
Test s velocity_15_5_4
Test s velocity_15_5_4Test s velocity_15_5_4
Test s velocity_15_5_4
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
 
Error Handling in Scala
Error Handling in ScalaError Handling in Scala
Error Handling in Scala
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84
 
Test (S) on R
Test (S) on RTest (S) on R
Test (S) on R
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Ray Tracing with ZIO
Ray Tracing with ZIORay Tracing with ZIO
Ray Tracing with ZIO
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game Programming
 
SOLID principles in practice: the Clean Architecture
SOLID principles in practice: the Clean ArchitectureSOLID principles in practice: the Clean Architecture
SOLID principles in practice: the Clean Architecture
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional Programming
 
Effector: we need to go deeper
Effector: we need to go deeperEffector: we need to go deeper
Effector: we need to go deeper
 
ScalaMeter 2012
ScalaMeter 2012ScalaMeter 2012
ScalaMeter 2012
 
C programs
C programsC programs
C programs
 
PROGRAM pod
PROGRAM podPROGRAM pod
PROGRAM pod
 

Similar to Reactive Collections

ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine Aleksandar Prokopec
 
The Ring programming language version 1.3 book - Part 16 of 88
The Ring programming language version 1.3 book - Part 16 of 88The Ring programming language version 1.3 book - Part 16 of 88
The Ring programming language version 1.3 book - Part 16 of 88Mahmoud Samir Fayed
 
Crushing the Head of the Snake by Robert Brewer PyData SV 2014
Crushing the Head of the Snake by Robert Brewer PyData SV 2014Crushing the Head of the Snake by Robert Brewer PyData SV 2014
Crushing the Head of the Snake by Robert Brewer PyData SV 2014PyData
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTVasil Remeniuk
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
 
Constraint Programming in Haskell
Constraint Programming in HaskellConstraint Programming in Haskell
Constraint Programming in HaskellDavid Overton
 
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84Mahmoud Samir Fayed
 
Apache Spark for Library Developers with William Benton and Erik Erlandson
 Apache Spark for Library Developers with William Benton and Erik Erlandson Apache Spark for Library Developers with William Benton and Erik Erlandson
Apache Spark for Library Developers with William Benton and Erik ErlandsonDatabricks
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bPhilip Schwarz
 
Matematika Dasar (exponen,dan banyak lagi)
Matematika Dasar (exponen,dan banyak lagi)Matematika Dasar (exponen,dan banyak lagi)
Matematika Dasar (exponen,dan banyak lagi)Lufikome
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchAhmed BESBES
 
Sistemas de control para ingenieria 3ra edicion norman s. nise sol
Sistemas de control para ingenieria  3ra edicion  norman s. nise solSistemas de control para ingenieria  3ra edicion  norman s. nise sol
Sistemas de control para ingenieria 3ra edicion norman s. nise solNielsy Quiroga
 
Time Series Analysis and Mining with R
Time Series Analysis and Mining with RTime Series Analysis and Mining with R
Time Series Analysis and Mining with RYanchang Zhao
 

Similar to Reactive Collections (20)

ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine ScalaDays 2014 - Reactive Scala 3D Game Engine
ScalaDays 2014 - Reactive Scala 3D Game Engine
 
The Ring programming language version 1.3 book - Part 16 of 88
The Ring programming language version 1.3 book - Part 16 of 88The Ring programming language version 1.3 book - Part 16 of 88
The Ring programming language version 1.3 book - Part 16 of 88
 
ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
 
2.3 implicits
2.3 implicits2.3 implicits
2.3 implicits
 
Crushing the Head of the Snake by Robert Brewer PyData SV 2014
Crushing the Head of the Snake by Robert Brewer PyData SV 2014Crushing the Head of the Snake by Robert Brewer PyData SV 2014
Crushing the Head of the Snake by Robert Brewer PyData SV 2014
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWT
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
Taller 2. 4264. (2)
Taller 2. 4264. (2)Taller 2. 4264. (2)
Taller 2. 4264. (2)
 
Constraint Programming in Haskell
Constraint Programming in HaskellConstraint Programming in Haskell
Constraint Programming in Haskell
 
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84
 
Reactive x
Reactive xReactive x
Reactive x
 
Ch4
Ch4Ch4
Ch4
 
Apache Spark for Library Developers with William Benton and Erik Erlandson
 Apache Spark for Library Developers with William Benton and Erik Erlandson Apache Spark for Library Developers with William Benton and Erik Erlandson
Apache Spark for Library Developers with William Benton and Erik Erlandson
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
 
Scala intro workshop
Scala intro workshopScala intro workshop
Scala intro workshop
 
Matematika Dasar (exponen,dan banyak lagi)
Matematika Dasar (exponen,dan banyak lagi)Matematika Dasar (exponen,dan banyak lagi)
Matematika Dasar (exponen,dan banyak lagi)
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from Scratch
 
Sistemas de control para ingenieria 3ra edicion norman s. nise sol
Sistemas de control para ingenieria  3ra edicion  norman s. nise solSistemas de control para ingenieria  3ra edicion  norman s. nise sol
Sistemas de control para ingenieria 3ra edicion norman s. nise sol
 
Servo systems
Servo systemsServo systems
Servo systems
 
Time Series Analysis and Mining with R
Time Series Analysis and Mining with RTime Series Analysis and Mining with R
Time Series Analysis and Mining with R
 

Recently uploaded

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 

Reactive Collections

  • 1. 1 Containers and Aggregates, Mutators and Isolates for Reactive Programming Aleksandar Prokopec, Philipp Haller, Martin Odersky
  • 4. 4
  • 6. 6 Observables (event streams) • declarative val log = messages .filter(_.length < 100) .scan(_ + “n” + _)
  • 7. 7 Observables (event streams) • declarative val log = messages .filter(_.length < 100) .scan(_ + “n” + _) var log = “” def receive = { case s: String => if (s.length < 100) log = log + “n” + s }
  • 8. 8 Actors • encapsulate mutable state
  • 9. 9 Actors • encapsulate mutable state var log = “” def receive = { case s: String => if (s.length < 100) log = log + “n” + s }
  • 10. 10 Reactive collections Isolate Reactive Channel Actor ? ActorRef ? Observable Observable
  • 13. val ticks: Reactive[Long] 13 ticks 1 1 2 2 3 3 4 4 60 60 61 61
  • 14. ticks onEvent { x => log.debug(s”tick no.$x”) } 14 1 2 3 4 60 61 tick no.1 tick no.2 tick no.3 tick no.4 tick no.60 tick no.61 ...
  • 15. ticks foreach { x => log.debug(s”tick no.$x”) } 15 1 2 3 4 60 61
  • 16. 16 for (x <- ticks) { log.debug(s”tick no.$x”) } Single-threaded!
  • 18. for (x <- ticks) yield { x / 60 } 18
  • 19. val seconds: Reactive[Long] = for (x <- ticks) yield { x / 60 } 19
  • 20. 60 61 val seconds: Reactive[Long] = for (x <- ticks) yield { x / 60 } 20 ticks 1 1 2 2 3 3 60 61 seconds 0 0 0 1 1 ticks seconds 0 1
  • 21. val days: Reactive[Long] = seconds.map(_ / 86400) 21
  • 22. val days: Reactive[Long] = seconds.map(_ / 86400) val secondsToday = 22
  • 23. val days: Reactive[Long] = seconds.map(_ / 86400) val secondsToday = (seconds zip days) { (s, d) => s – d * 86400 } 23
  • 24. val angle = secondsInDay.map(angleFunc) 24
  • 25. val angle = secondsInDay.map(angleFunc) val light = secondsInDay.map(lightFunc) 25
  • 26. 26
  • 27. 27 val rotate = keys a ↓ shift ↓ a ↑ shift ↑ pgup ↓ pgup ↑ keys
  • 28. 28 val rotate = keys.filter(_ == PAGEUP) a ↓ shift ↓ a ↑ shift ↑ pgup ↓ pgup ↑ keys pgup ↓ pgup ↑ filter
  • 29. 29 val rotate = keys.filter(_ == PAGEUP) .map(_.down) a ↓ shift ↓ a ↑ shift ↑ pgup ↓ pgup ↑ keys pgup ↓ pgup ↑ filter true false map
  • 30. 30 if (rotate()) viewAngle += 1 true false map
  • 32. 32 trait Signal[T] extends Reactive[T] { def apply(): T }
  • 33. 33 val rotate = keys.filter(_ == PAGEUP) .map(_.down) .signal(false) true false map signal
  • 34. 34 val rotate: Signal[Boolean] = keys.filter(_ == PAGEUP) .map(_.down) .signal(false) true false map signal
  • 35. 35 val rotate: Signal[Boolean] val ticks: Reactive[Long] ticks
  • 36. 36 val rotate: Signal[Boolean] val ticks: Reactive[Long] ticks rotate
  • 37. 37 val rotate: Signal[Boolean] val ticks: Reactive[Long] val viewAngle: Signal[Double] = ticks rotate viewAngle
  • 38. 38 val rotate: Signal[Boolean] val ticks: Reactive[Long] val viewAngle: Signal[Double] = ticks.scanPast(0.0) ticks rotate viewAngle
  • 39. 39 val rotate: Signal[Boolean] val ticks: Reactive[Long] val viewAngle: Signal[Double] = ticks.scanPast(0.0) { (a, _) => if (rotate()) a + 1 else a } ticks rotate viewAngle
  • 40. 40
  • 41. 41 val velocity = ticks.scanPast(0.0) { (v, _) => } val viewAngle =
  • 42. 42 val velocity = ticks.scanPast(0.0) { (v, _) => if (rotate()) v + 1 } val viewAngle =
  • 43. 43 val velocity = ticks.scanPast(0.0) { (v, _) => if (rotate()) v + 1 else v – 0.5 } val viewAngle =
  • 44. 44 val velocity = ticks.scanPast(0.0) { (v, _) => if (rotate()) v + 1 else v – 0.5 } val viewAngle = velocity.scanPast(0.0)(_ + _)
  • 45. 45
  • 47. 47 class Matrix { def apply(x: Int, y: Int): Double def update(x: Int, y: Int, v: Double) }
  • 48. 48 val screenMat: Signal[Matrix] = (projMat zip viewMat)(_ * _) val invScreenMat = screenMat.map(_.inverse)
  • 50. 50 val screenMat: Signal[Matrix] = (projMat zip viewMat)(_ * _) val invScreenMat = screenMat.map(_.inverse) (4*4*8 + 16 + 16)*4*100 = 64 kb/s
  • 51. 51 val screenMat = Mutable(new Matrix) (projMat, viewMat).mutate(screenMat) { (p, v) => screenMat().assignMul(p, v) } val invScreenMat = Mutable(new Matrix) screenMat.mutate(invScreenMat) { m => invScreenMat().assignInv(m) }
  • 53. 53
  • 54. 54 val selected: Reactive[Set[Character]]
  • 55. 55 val selected: ReactSet[Character]
  • 56. 56 trait ReactSet[T] extends ReactContainer[T] { def apply(x: T): Boolean }
  • 57. 57 trait ReactContainer[T] { def inserts: Reactive[T] def removes: Reactive[T] }
  • 58. 58 A reactive collection is a pair of reactive values
  • 59. 59 val selected = new ReactHashSet[Character]
  • 60. 60
  • 61. 61 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c)))
  • 62. 62 class ReactContainer[T] { self => def inserts: Reactive[T] def removes: Reactive[T] def map[S](f: T => S) = new ReactContainer[S] { def inserts: Reactive[T] = self.inserts.map(f) def removes: Reactive[T] = self.removes.map(f) } }
  • 63. 63 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
  • 64. 64 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
  • 65. 65 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
  • 66. 66 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .to[ReactHashMap]
  • 67. 67 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
  • 68. 68 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
  • 69. 69 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
  • 70. 70 val selected = new ReactHashSet[Character] val decorations = selected .map(c => (c, decoFor(c))) .react.to[ReactHashMap]
  • 72. 72 UI isolate class UI extends Isolate[UI.Message] { val frames = source.filter(_ == UI.Frame) val exit = source onCase { case UI.Exit => exit() } } Source
  • 73. 73 UI Isolate Source AI Isolate Source Channel[AI.Message]   Channel[UI.Message]
  • 74. 74 Reactive collections Isolate Reactive Channel Actor ? ActorRef ? Observable Observable