Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
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...
We need… 
• To move quickly, be agile, adapt to changes 
• Performant code 
• To leverage concurrency 
• To rent server sp...
• 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 typ...
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 c...
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 ...
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  Conc...
Functional Programming and Concurrency Patterns in Scala
Functional Programming and Concurrency Patterns in Scala
Prochain SlideShare
Chargement dans…5
×

Functional Programming and Concurrency Patterns in Scala

1 866 vues

Publié le

An introduction to Scala, Functional Programming, and concurrency patterns that I gave to a class of students at Houghton College on November 2, 2014

Publié dans : Ingénierie
  • Soyez le premier à commenter

Functional Programming and Concurrency Patterns in Scala

  1. 1. Functional Programming & Concurrency Patterns Tim Kellogg (@kellogh) September 2, 2014
  2. 2. Agenda • Introduce 2lemetry & IoT technologies • Introduce Scala • Functional Programming • Concurrency patterns
  3. 3. About Me • 39.9747597,-105.0881511 • Class of 2007
  4. 4. Internet of Things • Moving • Storing • Analyzing • Reacting
  5. 5. MQTT • Message Queue Telemetry Transport • Pub/Sub binary protocol • Open standard • Persistent connections
  6. 6. 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
  7. 7. We need… • To move quickly, be agile, adapt to changes • Performant code • To leverage concurrency • To rent server space in the cloud
  8. 8. • Functional + Object Oriented • Strongly typed – Safe – Not like Java types • JVM
  9. 9. • First-class functions • Traits • Type inference • Record types (case classes) • Pattern Matching (and structural typing)
  10. 10. Record Types
  11. 11. Pattern Matching
  12. 12. First class functions
  13. 13. First Class Functions
  14. 14. Example: Count all goals from young players
  15. 15. Example: Count Goals
  16. 16. Example: Count Goals
  17. 17. Example: Count Goals
  18. 18. Example: Count Goals
  19. 19. Example: Count Goals
  20. 20. Example: Count Goals
  21. 21. Example: Count Goals
  22. 22. Example: Count Goals
  23. 23. Example: Count Goals
  24. 24. Example: Count Goals
  25. 25. Example: Count Goals
  26. 26. FP Mainstream References • SQL • LINQ (C#) • jQuery • Hadoop (map / reduce) – Spark
  27. 27. Monads
  28. 28. Common Monads • List[T] • Set[T] • Option[T]  No more nulls • Future[T] • Try[T]
  29. 29. Monads • Container • “bind” operation – “flatMap” in Scala – T => M[U]
  30. 30. List[T] As A Monad
  31. 31. Option[T] As A Monad
  32. 32. Option[T] As Monad
  33. 33. List[T] As A Monad
  34. 34. List[T] As A Monad
  35. 35. Concurrency
  36. 36. Concurrency Basics • If it’s immutable, it’s thread-safe • Locks are hard to reason about • Badly placed locks defeat concurrency
  37. 37. Multi-threading vs Concurrent • Concurrent – An architecture – Not necessarily multi-threaded
  38. 38. Immutability: What You Can’t Do • Change a variable • Write to a file • Write to a TCP socket
  39. 39. Haskell Is Useless Simon Peyton Jones https://www.youtube.com/ watch?v=iSmkqocn0oQ
  40. 40. Side Note: Reference Immutability • https://homes.cs.washington.edu/~csgordon/ papers/oopsla12.pdf • M# • Rust
  41. 41. Immutability: What You Can Do • State Monad • IO Monad • Future Monad • Forget about concurrency details
  42. 42. Role of Monads • Represent state as a value • Declarative programming – Describe the problem – Let the compiler solve it for you
  43. 43. Future[T] • Represents a value • Handles failure as a value • Computation might happen on another thread • Supervisors
  44. 44. Future[T] • map / filter • flatMap (bind) • successful (pure)
  45. 45. Future[T]
  46. 46. Future[T] - Dispatchers • Thread pooling • Configuration
  47. 47. Actors
  48. 48. Actors • Akka (Scala) • OTP (Erlang) • http://arxiv.org/pdf/1008.1459.pdf • “Sandboxing” mutability & side effects
  49. 49. Actor Model of Computation
  50. 50. Actor Mailbox
  51. 51. Akka
  52. 52. Akka Specialization • Actors  Computation • Dispatchers  Thread pooling • Supervisors  Reliability • Routers  Concurrency

×