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
@Jorgesg1986
実例で解説する関数型プログラミングを用いたコードの改善
Background
Skills
Time
Acceptable
Mainframe
Coursera
Habla Computing
Our first requirement as a Scala
developer
• We work for an analytics company.
• As a user I would like to see if bad or g...
Our company’s architecture
弊社のアーキテクチ
ャ
Our company’s architecture
自然言語解析とかも Spark 経由で
After some investigation...
• Spark for stream processing.
• Stanford CoreNLP for Sentiment Analysis.
• Kafka for communic...
Defining our data model
tweet に対応するデータモデ
Error handling
• Scala Standard Library:
– Option[T]: Some(T) or None
– Try: Failure(Non-Fatal) or Success(T)
– Either[A, ...
Error handling
• Cats:
– Either: Same as standard. Right biased.
– Validated: Useful to accumulate errors.
• ScalaZ:
– /: ...
Revisiting our first approach
Let’s see how that works...
All the work is done now…
これで仕事は終わ
そんなはずはない
Testing it
• Deploy to some development environment and test.
開発環境にデプロイしてテス
ト
Testing it
• Deploy to some development environment and test.
• Refactor the code.
リファクタリン
Improving our first approach
• Try to look only for your Business Logic.
• Separate from the specifics of your implementat...
Our Business Logic
• Filter the tweets that are not written in English.
• Obtain the location from where the tweet was sen...
Refactoring the Avro serialization
Avro のシリアライズのリファクタリン
グ
Separating the logic
ビジネスロジックの分離
Enter for-comprehensions
for- 内包表記登場
And test!
• You can now test ONLY your Business Logic without caring about your
implementation (In this case Spark).
• Use...
Keep testing!
• What about integration tests?
結合テストは?
What happens if...
• The framework changes?
フレームワークが変わった場合どうなるでしょ
う?
What happens if...
• The framework changes?
• Akka Streams
Akka Streams の場
合
Akka Streams
• Processing Stages
• Source, Flow, Sink
Akka Streams
Source Sink
Flow
Akka Streams
Source Sink
Flow
Output port
Output port
Akka Streams
Source Sink
Flow
Output port
Output port
Input port
Input port
Akka Streams
Source Sink
Flow
Output port
Output port
Input port
Input port
Akka Streams
Running Asynchronously
• Using Scala Futures
非同期に実行する場合
Running Asynchronously
“It represents space: a value, detached from time”
Viktor Klang
それは空間、つまり時間から分離した値を表
す。
Revisiting our first approach
• Run our business logic asynchronously.
最初の方法をおさらい
各ビジネスロジックを非同期に実行
Revisiting our first approach
Monad
モナド
Revisiting our first approach
Monad
Applicative Functor
アプリカティブ・ファンクタ
ー
Revisiting our first approach
Monad
Applicative Functor
Revisiting our first approach
Monad
Applicative Functor
Revisiting our first approach
• Using Monad Transformers to compose Monads.
Revisiting our first approach
• Controlling the execution.
• Using a different Monad: Monix Task, ScalaZ IO...
実行モデルを制御
Revisiting our first approach
• Swapping Future with Task.
Future をタスクに入れ替え
Adding a level of abstraction
抽象化層を追加
Running it with Future/Task
Future/Task それぞれで実
And testing it
それをテストしてみ
る
Wait a moment...
We can use Applicative!!
Applicative も使えます
And run for Future, Task...
Future とタスクそれぞれで走らせ
る
And test it!
それもテスト
Summary
• Error Handling
• Testability
• Readability (For-comprehensions)
• Reusability
• Monad / Applicative
All the work is done now
*Disclaimer: No Cats or Trumps were harmed during the making of this presentation
ありがとうございます !
Thank you very much!
Using Functional Programming to improve your code: A working example
Using Functional Programming to improve your code: A working example
Using Functional Programming to improve your code: A working example
Using Functional Programming to improve your code: A working example
Upcoming SlideShare
Loading in …5
×

of

Using Functional Programming to improve your code: A working example Slide 1 Using Functional Programming to improve your code: A working example Slide 2 Using Functional Programming to improve your code: A working example Slide 3 Using Functional Programming to improve your code: A working example Slide 4 Using Functional Programming to improve your code: A working example Slide 5 Using Functional Programming to improve your code: A working example Slide 6 Using Functional Programming to improve your code: A working example Slide 7 Using Functional Programming to improve your code: A working example Slide 8 Using Functional Programming to improve your code: A working example Slide 9 Using Functional Programming to improve your code: A working example Slide 10 Using Functional Programming to improve your code: A working example Slide 11 Using Functional Programming to improve your code: A working example Slide 12 Using Functional Programming to improve your code: A working example Slide 13 Using Functional Programming to improve your code: A working example Slide 14 Using Functional Programming to improve your code: A working example Slide 15 Using Functional Programming to improve your code: A working example Slide 16 Using Functional Programming to improve your code: A working example Slide 17 Using Functional Programming to improve your code: A working example Slide 18 Using Functional Programming to improve your code: A working example Slide 19 Using Functional Programming to improve your code: A working example Slide 20 Using Functional Programming to improve your code: A working example Slide 21 Using Functional Programming to improve your code: A working example Slide 22 Using Functional Programming to improve your code: A working example Slide 23 Using Functional Programming to improve your code: A working example Slide 24 Using Functional Programming to improve your code: A working example Slide 25 Using Functional Programming to improve your code: A working example Slide 26 Using Functional Programming to improve your code: A working example Slide 27 Using Functional Programming to improve your code: A working example Slide 28 Using Functional Programming to improve your code: A working example Slide 29 Using Functional Programming to improve your code: A working example Slide 30 Using Functional Programming to improve your code: A working example Slide 31 Using Functional Programming to improve your code: A working example Slide 32 Using Functional Programming to improve your code: A working example Slide 33 Using Functional Programming to improve your code: A working example Slide 34 Using Functional Programming to improve your code: A working example Slide 35 Using Functional Programming to improve your code: A working example Slide 36 Using Functional Programming to improve your code: A working example Slide 37 Using Functional Programming to improve your code: A working example Slide 38 Using Functional Programming to improve your code: A working example Slide 39 Using Functional Programming to improve your code: A working example Slide 40 Using Functional Programming to improve your code: A working example Slide 41 Using Functional Programming to improve your code: A working example Slide 42 Using Functional Programming to improve your code: A working example Slide 43 Using Functional Programming to improve your code: A working example Slide 44 Using Functional Programming to improve your code: A working example Slide 45 Using Functional Programming to improve your code: A working example Slide 46 Using Functional Programming to improve your code: A working example Slide 47 Using Functional Programming to improve your code: A working example Slide 48 Using Functional Programming to improve your code: A working example Slide 49 Using Functional Programming to improve your code: A working example Slide 50 Using Functional Programming to improve your code: A working example Slide 51 Using Functional Programming to improve your code: A working example Slide 52 Using Functional Programming to improve your code: A working example Slide 53 Using Functional Programming to improve your code: A working example Slide 54
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

1 Like

Share

Download to read offline

Using Functional Programming to improve your code: A working example

Download to read offline

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

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

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!
  • emilioacor

    Aug. 29, 2019

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

Views

Total views

686

On Slideshare

0

From embeds

0

Number of embeds

5

Actions

Downloads

3

Shares

0

Comments

0

Likes

1

×