SlideShare a Scribd company logo
1 of 54
Functional Programming & 
Concurrency Patterns 
Tim Kellogg (@kellogh) 
September 2, 2014
Agenda 
• Introduce 2lemetry & IoT technologies 
• Introduce Scala 
• Functional Programming 
• Concurrency patterns
About Me 
• 39.9747597,-105.0881511 
• Class of 2007
Internet of Things 
• Moving 
• Storing 
• Analyzing 
• Reacting
MQTT 
• Message Queue Telemetry Transport 
• Pub/Sub binary protocol 
• Open standard 
• Persistent connections
IoT-SQL 
SELECT LAST(*) 
FROM mqtt(‘co.t/temp/+’) AS temp 
JOIN storage(‘metadata’) AS md 
ON temp.deviceId = md.id 
WHERE temp.outside.value > 90 
AND temp.inside.value < 70 
GROUP BY 1 HOUR
We need… 
• To move quickly, be agile, adapt to changes 
• Performant code 
• To leverage concurrency 
• To rent server space in the cloud
• Functional + Object Oriented 
• Strongly typed 
– Safe 
– Not like Java types 
• JVM
• First-class functions 
• Traits 
• Type inference 
• Record types (case classes) 
• Pattern Matching (and structural typing)
Record Types
Pattern Matching
First class functions
First Class Functions
Example: 
Count all goals from young players
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
Example: Count Goals
FP Mainstream References 
• SQL 
• LINQ (C#) 
• jQuery 
• Hadoop (map / reduce) 
– Spark
Monads
Common Monads 
• List[T] 
• Set[T] 
• Option[T]  No more nulls 
• Future[T] 
• Try[T]
Monads 
• Container 
• “bind” operation 
– “flatMap” in Scala 
– T => M[U]
List[T] As A Monad
Option[T] As A Monad
Option[T] As Monad
List[T] As A Monad
List[T] As A Monad
Concurrency
Concurrency Basics 
• If it’s immutable, it’s thread-safe 
• Locks are hard to reason about 
• Badly placed locks defeat concurrency
Multi-threading vs Concurrent 
• Concurrent 
– An architecture 
– Not necessarily multi-threaded
Immutability: What You Can’t Do 
• Change a variable 
• Write to a file 
• Write to a TCP socket
Haskell Is 
Useless 
Simon Peyton Jones 
https://www.youtube.com/ 
watch?v=iSmkqocn0oQ
Side Note: Reference Immutability 
• https://homes.cs.washington.edu/~csgordon/ 
papers/oopsla12.pdf 
• M# 
• Rust
Immutability: What You Can Do 
• State Monad 
• IO Monad 
• Future Monad 
• Forget about concurrency details
Role of Monads 
• Represent state as a value 
• Declarative programming 
– Describe the problem 
– Let the compiler solve it for you
Future[T] 
• Represents a value 
• Handles failure as a value 
• Computation might happen on another thread 
• Supervisors
Future[T] 
• map / filter 
• flatMap (bind) 
• successful (pure)
Future[T]
Future[T] - Dispatchers 
• Thread pooling 
• Configuration
Actors
Actors 
• Akka (Scala) 
• OTP (Erlang) 
• http://arxiv.org/pdf/1008.1459.pdf 
• “Sandboxing” mutability & side effects
Actor Model of Computation
Actor Mailbox
Akka
Akka Specialization 
• Actors  Computation 
• Dispatchers  Thread pooling 
• Supervisors  Reliability 
• Routers  Concurrency
Functional Programming and Concurrency Patterns in Scala

More Related Content

What's hot

Encryption oracle
Encryption oracleEncryption oracle
Encryption oracle
manong007
 

What's hot (9)

In a Nutshell: Rancher
In a Nutshell: RancherIn a Nutshell: Rancher
In a Nutshell: Rancher
 
Encryption oracle
Encryption oracleEncryption oracle
Encryption oracle
 
KubeCon EU 2019 - P2P Docker Image Distribution in Hybrid Cloud Environment w...
KubeCon EU 2019 - P2P Docker Image Distribution in Hybrid Cloud Environment w...KubeCon EU 2019 - P2P Docker Image Distribution in Hybrid Cloud Environment w...
KubeCon EU 2019 - P2P Docker Image Distribution in Hybrid Cloud Environment w...
 
Dock-site
Dock-siteDock-site
Dock-site
 
Interpolation Generalized
Interpolation GeneralizedInterpolation Generalized
Interpolation Generalized
 
Roots of Nonlinear Equations - Open Methods
Roots of Nonlinear Equations - Open MethodsRoots of Nonlinear Equations - Open Methods
Roots of Nonlinear Equations - Open Methods
 
Node Architecture.pptx
Node Architecture.pptxNode Architecture.pptx
Node Architecture.pptx
 
Functional Programming With Elixir
Functional Programming With ElixirFunctional Programming With Elixir
Functional Programming With Elixir
 
Asynchronous Programming with Kotlin
Asynchronous Programming with KotlinAsynchronous Programming with Kotlin
Asynchronous Programming with Kotlin
 

Viewers also liked

Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
NLJUG
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
mircodotta
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
mircodotta
 

Viewers also liked (20)

Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
 
Practical type mining in Scala
Practical type mining in ScalaPractical type mining in Scala
Practical type mining in Scala
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Scala Types of Types @ Lambda Days
Scala Types of Types @ Lambda DaysScala Types of Types @ Lambda Days
Scala Types of Types @ Lambda Days
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scala’s implicits
Scala’s implicitsScala’s implicits
Scala’s implicits
 
Scala Implicits - Not to be feared
Scala Implicits - Not to be fearedScala Implicits - Not to be feared
Scala Implicits - Not to be feared
 
Akka Fundamentals
Akka FundamentalsAkka Fundamentals
Akka Fundamentals
 
Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
 
Scal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and AngularScal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and Angular
 
Akka Actor presentation
Akka Actor presentationAkka Actor presentation
Akka Actor presentation
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
 
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
 
Akka in 100 slides or less
Akka in 100 slides or lessAkka in 100 slides or less
Akka in 100 slides or less
 
Introducing Reactive Machine Learning
Introducing Reactive Machine LearningIntroducing Reactive Machine Learning
Introducing Reactive Machine Learning
 
Building Reactive Systems with Akka (in Java 8 or Scala)
Building Reactive Systems with Akka (in Java 8 or Scala)Building Reactive Systems with Akka (in Java 8 or Scala)
Building Reactive Systems with Akka (in Java 8 or Scala)
 
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
 
How to deploy Apache Spark 
to Mesos/DCOS
How to deploy Apache Spark 
to Mesos/DCOSHow to deploy Apache Spark 
to Mesos/DCOS
How to deploy Apache Spark 
to Mesos/DCOS
 

Similar to Functional Programming and Concurrency Patterns in Scala

Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm
Chandler Huang
 
Performance optimization - JavaScript
Performance optimization - JavaScriptPerformance optimization - JavaScript
Performance optimization - JavaScript
Filip Mares
 
Rust All Hands Winter 2011
Rust All Hands Winter 2011Rust All Hands Winter 2011
Rust All Hands Winter 2011
Patrick Walton
 

Similar to Functional Programming and Concurrency Patterns in Scala (20)

The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Aca2 09 new
Aca2 09 newAca2 09 new
Aca2 09 new
 
Unit 1- dbms.ppt
Unit 1- dbms.pptUnit 1- dbms.ppt
Unit 1- dbms.ppt
 
Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm
 
Reactive Software Systems
Reactive Software SystemsReactive Software Systems
Reactive Software Systems
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
dbms (1).ppt
dbms (1).pptdbms (1).ppt
dbms (1).ppt
 
Performance optimization - JavaScript
Performance optimization - JavaScriptPerformance optimization - JavaScript
Performance optimization - JavaScript
 
Storm at Forter
Storm at ForterStorm at Forter
Storm at Forter
 
Dbms
DbmsDbms
Dbms
 
Stackato v2
Stackato v2Stackato v2
Stackato v2
 
Rust All Hands Winter 2011
Rust All Hands Winter 2011Rust All Hands Winter 2011
Rust All Hands Winter 2011
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Internals of Presto Service
Internals of Presto ServiceInternals of Presto Service
Internals of Presto Service
 
John adams talk cloudy
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudy
 
Dbms
DbmsDbms
Dbms
 
Apache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide DeckApache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide Deck
 
Stackato v4
Stackato v4Stackato v4
Stackato v4
 

More from kellogh

More from kellogh (9)

Biologically Inspired Internet of Things
Biologically Inspired Internet of ThingsBiologically Inspired Internet of Things
Biologically Inspired Internet of Things
 
ThingMonk 2014: How To Improve On MQTT 3.1.1
ThingMonk 2014: How To Improve On MQTT 3.1.1ThingMonk 2014: How To Improve On MQTT 3.1.1
ThingMonk 2014: How To Improve On MQTT 3.1.1
 
Why HTTP Won't Work For The Internet of Things (Dreamforce 2014)
Why HTTP Won't Work For The Internet of Things (Dreamforce 2014)Why HTTP Won't Work For The Internet of Things (Dreamforce 2014)
Why HTTP Won't Work For The Internet of Things (Dreamforce 2014)
 
Security & Identity in AllJoyn 14.06
Security & Identity in AllJoyn 14.06Security & Identity in AllJoyn 14.06
Security & Identity in AllJoyn 14.06
 
Programming The Arduino Due in Rust
Programming The Arduino Due in RustProgramming The Arduino Due in Rust
Programming The Arduino Due in Rust
 
Why HTTP Won't Work For The Internet of Things
Why HTTP Won't Work For The Internet of ThingsWhy HTTP Won't Work For The Internet of Things
Why HTTP Won't Work For The Internet of Things
 
Comparing CoAP vs MQTT
Comparing CoAP vs MQTTComparing CoAP vs MQTT
Comparing CoAP vs MQTT
 
Scaling MQTT With Apache Kafka
Scaling MQTT With Apache KafkaScaling MQTT With Apache Kafka
Scaling MQTT With Apache Kafka
 
Internet of things, lafayette tech
Internet of things, lafayette techInternet of things, lafayette tech
Internet of things, lafayette tech
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Recently uploaded (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 

Functional Programming and Concurrency Patterns in Scala

Editor's Notes

  1. Sealed means nothing can extend it outside this file Generated equals, getHashCode, copy, apply & unapply Sum types
  2. Also called lambda functions
  3. Let’s try to show the advantages of functional programming
  4. Imperative
  5. All statements are values Add an `else` block
  6. Use foreach-style loop
  7. Add filter to `for` comprehension
  8. Use filter + map + sum
  9. Use short-hand lambda syntax
  10. One line
  11. Remove return type annotation & braces
  12. Try making this into a function-value
  13. Use foldLeft instead of map + sum *faster* since it only creates 2 lists
  14. Use lambdas on the foldLeft
  15. No type constraints on the container bind  flatMap, >>=
  16. Node.js -> libuv