Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Functional Programming and Concurrency Patterns in Scala

1.854 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Ingenieurwesen
  • Als Erste(r) kommentieren

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

×