Upload
kellogh
View
330
Download
3
Tags:
Embed Size (px)
DESCRIPTION
An introduction to Scala, Functional Programming, and concurrency patterns that I gave to a class of students at Houghton College on November 2, 2014
Citation preview
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-SQLSELECT LAST(*) FROM mqtt(‘co.t/temp/+’) AS tempJOIN storage(‘metadata’) AS md ON temp.deviceId = md.idWHERE temp.outside.value > 90 AND temp.inside.value < 70GROUP 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 Joneshttps://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