Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using Functional Programming to improve your code: A working example

323 views

Published on

Slides for the presentation at Scala Matsuri 2018. Code available at https://github.com/jorgesg1986/ScalaMatsuri

Published in: Software
  • Be the first to comment

  • Be the first to like this

Using Functional Programming to improve your code: A working example

  1. 1. Using Functional Programming to improve your code: A working example @Jorgesg1986 実例で解説する関数型プログラミングを用いたコードの改善
  2. 2. Background Skills Time Acceptable Mainframe Coursera Habla Computing
  3. 3. Our first requirement as a Scala developer • We work for an analytics company. • As a user I would like to see if bad or good things are being said about Trump and where. 最初の案件 : トランプ大統領の風評分
  4. 4. Our company’s architecture 弊社のアーキテクチ ャ
  5. 5. Our company’s architecture 自然言語解析とかも Spark 経由で
  6. 6. After some investigation... • Spark for stream processing. • Stanford CoreNLP for Sentiment Analysis. • Kafka for communication between servers. • Avro4s for Avro serialization. 各要素の役割
  7. 7. Defining our data model tweet に対応するデータモデ
  8. 8. Error handling • Scala Standard Library: – Option[T]: Some(T) or None – Try: Failure(Non-Fatal) or Success(T) – Either[A, B]: Left(A) or Right(B). Unbiased prior to 2.12 Scala 標準ライブラリのエラーハンドリン
  9. 9. Error handling • Cats: – Either: Same as standard. Right biased. – Validated: Useful to accumulate errors. • ScalaZ: – /: Either. Right biased. – Validation: Useful to accumulate errors. Cats, Scalaz の場合
  10. 10. Revisiting our first approach
  11. 11. Let’s see how that works...
  12. 12. All the work is done now… これで仕事は終わ
  13. 13. そんなはずはない
  14. 14. Testing it • Deploy to some development environment and test. 開発環境にデプロイしてテス ト
  15. 15. Testing it • Deploy to some development environment and test. • Refactor the code. リファクタリン
  16. 16. Improving our first approach • Try to look only for your Business Logic. • Separate from the specifics of your implementation. ビジネスロジックを分離
  17. 17. Our Business Logic • Filter the tweets that are not written in English. • Obtain the location from where the tweet was sent. • Analyze the text to extract the sentiment. • Return a new instance of type Tweet. • Extra: Refactor the Avro serialization. 英語の tweet のみ抽出、ロケーションの取得、テキストから感情を分 析、
  18. 18. Refactoring the Avro serialization Avro のシリアライズのリファクタリン グ
  19. 19. Separating the logic ビジネスロジックの分離
  20. 20. Enter for-comprehensions for- 内包表記登場
  21. 21. And test! • You can now test ONLY your Business Logic without caring about your implementation (In this case Spark). • Use your choice of testing framework. ScalaTest, Specs2, µTest, Puretest, minitest... 実装に依存しないビジネスロジックのテストができました。
  22. 22. Keep testing! • What about integration tests? 結合テストは?
  23. 23. What happens if... • The framework changes? フレームワークが変わった場合どうなるでしょ う?
  24. 24. What happens if... • The framework changes? • Akka Streams Akka Streams の場 合
  25. 25. Akka Streams • Processing Stages • Source, Flow, Sink
  26. 26. Akka Streams Source Sink Flow
  27. 27. Akka Streams Source Sink Flow Output port Output port
  28. 28. Akka Streams Source Sink Flow Output port Output port Input port Input port
  29. 29. Akka Streams Source Sink Flow Output port Output port Input port Input port
  30. 30. Akka Streams
  31. 31. Running Asynchronously • Using Scala Futures 非同期に実行する場合
  32. 32. Running Asynchronously “It represents space: a value, detached from time” Viktor Klang それは空間、つまり時間から分離した値を表 す。
  33. 33. Revisiting our first approach • Run our business logic asynchronously. 最初の方法をおさらい 各ビジネスロジックを非同期に実行
  34. 34. Revisiting our first approach Monad モナド
  35. 35. Revisiting our first approach Monad Applicative Functor アプリカティブ・ファンクタ ー
  36. 36. Revisiting our first approach Monad Applicative Functor
  37. 37. Revisiting our first approach Monad Applicative Functor
  38. 38. Revisiting our first approach • Using Monad Transformers to compose Monads.
  39. 39. Revisiting our first approach • Controlling the execution. • Using a different Monad: Monix Task, ScalaZ IO... 実行モデルを制御
  40. 40. Revisiting our first approach • Swapping Future with Task. Future をタスクに入れ替え
  41. 41. Adding a level of abstraction 抽象化層を追加
  42. 42. Running it with Future/Task Future/Task それぞれで実
  43. 43. And testing it それをテストしてみ る
  44. 44. Wait a moment...
  45. 45. We can use Applicative!! Applicative も使えます
  46. 46. And run for Future, Task... Future とタスクそれぞれで走らせ る
  47. 47. And test it! それもテスト
  48. 48. Summary • Error Handling • Testability • Readability (For-comprehensions) • Reusability • Monad / Applicative
  49. 49. All the work is done now *Disclaimer: No Cats or Trumps were harmed during the making of this presentation
  50. 50. ありがとうございます ! Thank you very much!

×