FLATMAP ZAT SHITLes monades expliquées aux geeks      11h30 - 12h20 - Auditorium
FLATMAP ZAT SHITLes monades expliquées aux geeks        François Sarradin           Développeur λ           @fsarradin    ...
François Sarradin ● Développeur λ ● http://kerflyn.wordpress.com/                                                     @fsa...
Entity x = getEntity1()Entity y = getEntity2()Entity z = x.get() + y.get()
Entity x = getEntity1() Entity y = getEntity2() Exception Entity z = x.get() + y.get()                       nullAsynchrone
if                                          ()                   (?                                  ch                   ...
Entity x = getEntity1()Entity y = getEntity2()Entity z = x.get() + y.get()                               * Conserve le    ...
Agenda ●   Live coding / Scala 2.10     ○ Exception     ○ Asynchrone ●   Code review / Java 8     ○Exception ●   Conclusion
Alice       InBank-land
Alice
Service WebSolde total ?
Architecture3: PROFIT!                          Web                                       JSON2: ???                      ...
def balanceByBank: String = { val balancesByBankJson:   Iterable[String] = for ((bankName, accountNumbers        <- ownerA...
Try[A] val ok = Try { 1 } ok: scala.util.Try[Int] = Success(1) val ko = Try { throw new Exception("#1") } ko: scala.util.T...
1 Try[A] 1 for-expression for { x <- ok } yield s"x = $x" res1: scala.util.Try[String] = Success(x = 1) for { x <- ko } yi...
2 Try[A] 1 for-expression val   ok1:   Try[Int]   =   Success(1) val   ok2:   Try[Int]   =   Success(2) val   ko1:   Try[I...
def balanceByBank: String = { val balancesByBankJson:   Iterable[String] = for ((bankName, accountNumbers        <- ownerA...
Future[A] val fshort: Future[Int] = Future { 1 } fshort: Future[Int] = ... // illico val flong: Future[Int] = Future { Thr...
Future[A] & for-expression for { x <- fshort } yield s"x = $x" res1: Future[String] ≈ Future("x = 1") // in fine for { x <...
def balanceByBank: String = { val balancesByBankJson:   Iterable[String] = for ((bankName, accountNumbers        <- ownerA...
Type monadique      Try                                                          Future   (exception)                     ...
flatMap : opération monadiquefor {                      m.map(x =>  x <- m                     x + 1}                     ...
def balanceByBank: String = { val balancesByBankJson:   Iterable[String] = for ((bankName, accountNumbers        <- ownerA...
Conclusion ●   Code métier     ○Peu de changement ●   Système de type     ○Aspect technique (déclaratif)     ○Composition ...
Question ?
Upcoming SlideShare
Loading in …5
×

FLATMAP ZAT SHIT : les monades expliquées aux geeks (Devoxx France 2013)

909 views

Published on

Vous commencez à peine dans la programmation fonctionnelle avec Scala, Java 8, CoffeeScript, etc. et on vous sort déjà des noms à dormir debout. Parmi ceux-ci, il en a un qui fait la joie des Scalaïstes les plus velus : je veux parler des monades.

Dans cette session, je vous propose de découvrir ce qu'est une monade, à quoi ça sert et est-ce qu'il y a un intérêt de les étudier... ou pas ! La présentation qui contient plus de code que de slides se fera autour de deux langages : Java 8 et Scala.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
909
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

FLATMAP ZAT SHIT : les monades expliquées aux geeks (Devoxx France 2013)

  1. 1. FLATMAP ZAT SHITLes monades expliquées aux geeks 11h30 - 12h20 - Auditorium
  2. 2. FLATMAP ZAT SHITLes monades expliquées aux geeks François Sarradin Développeur λ @fsarradin 27 au 29 mars 2013
  3. 3. François Sarradin ● Développeur λ ● http://kerflyn.wordpress.com/ @fsarradin ● "Je suis un bagger" ● http://www.brownbaglunch.fr/ @bbl_fr
  4. 4. Entity x = getEntity1()Entity y = getEntity2()Entity z = x.get() + y.get()
  5. 5. Entity x = getEntity1() Entity y = getEntity2() Exception Entity z = x.get() + y.get() nullAsynchrone
  6. 6. if () (? ch != c atif ( nu ... ? == ll null ry ) Entity ) = getEntity1() lly t ina x y . .. f tr Entity y = getEntity2() nu ll) (? != Entity z = x.get() + y.get() tryif .. . ca tch( sy z ed ) ad oni nc re hr Th z . h Th nc ro re ed ni .. sy . ad ..
  7. 7. Entity x = getEntity1()Entity y = getEntity2()Entity z = x.get() + y.get() * Conserve le code métier
  8. 8. Agenda ● Live coding / Scala 2.10 ○ Exception ○ Asynchrone ● Code review / Java 8 ○Exception ● Conclusion
  9. 9. Alice InBank-land
  10. 10. Alice
  11. 11. Service WebSolde total ?
  12. 12. Architecture3: PROFIT! Web JSON2: ??? BankService getAccount(b, n) a:Account AccountRepository1: Get accounts BankProxy BankProxy BankProxy BGP La Postale Breizh Bank
  13. 13. def balanceByBank: String = { val balancesByBankJson: Iterable[String] = for ((bankName, accountNumbers <- ownerAccounts) yield { val balances: List[Double] = for (accountNumber <- accountNumbers.toList) yield getAccount(bankName, accountNumber ).balance Démonstration s"""{"name":"$bankNam Scala 2.10 e","b alan ce": "${b ala nce s. su m} "} " " Code source sur Github " } https://github.com/fsarradin/bankapp.git 27 au 29 mars 2013
  14. 14. Try[A] val ok = Try { 1 } ok: scala.util.Try[Int] = Success(1) val ko = Try { throw new Exception("#1") } ko: scala.util.Try[Nothing] = Failure(Exception: #1)
  15. 15. 1 Try[A] 1 for-expression for { x <- ok } yield s"x = $x" res1: scala.util.Try[String] = Success(x = 1) for { x <- ko } yield s"x = $x" res2: scala.util.Try[String] = Failure(Exception: #1)
  16. 16. 2 Try[A] 1 for-expression val ok1: Try[Int] = Success(1) val ok2: Try[Int] = Success(2) val ko1: Try[Int] = Failure(new Exception("#1")) val ko2: Try[Int] = Failure(new Exception("#2")) for { x <- ok1; y <- ok2 } yield x + y res1: scala.util.Try[Int] = Success(3) for { x <- ko1; y <- ok2 } yield x + y res2: scala.util.Try[String] = Failure(Exception: #1) for { x <- ko1; y <- ko2 } yield x + y res3: scala.util.Try[String] = Failure(Exception: #1)
  17. 17. def balanceByBank: String = { val balancesByBankJson: Iterable[String] = for ((bankName, accountNumbers <- ownerAccounts) yield { val balances: List[Double] = for (accountNumber <- accountNumbers.toList) yield getAccount(bankName, accountNumber ).balance Démonstration s"""{"name":"$bankNam Scala 2.10 : Try e","b alan ce": "${b ala nce s. su m} "} " " Code source sur Github " } https://github.com/fsarradin/bankapp.git 27 au 29 mars 2013
  18. 18. Future[A] val fshort: Future[Int] = Future { 1 } fshort: Future[Int] = ... // illico val flong: Future[Int] = Future { Thread.sleep(2000); 2 } flong: Future[Int] = ... // illico
  19. 19. Future[A] & for-expression for { x <- fshort } yield s"x = $x" res1: Future[String] ≈ Future("x = 1") // in fine for { x <- fshort; y <- flong } yield x + y res2: Future[Int] ≈ Future(3) // in fine
  20. 20. def balanceByBank: String = { val balancesByBankJson: Iterable[String] = for ((bankName, accountNumbers <- ownerAccounts) yield { val balances: List[Double] = for (accountNumber <- accountNumbers.toList) yield getAccount(bankName, accountNumber ).balance Démonstration s"""{"name":"$bankNam Scala 2.10 : Future e","b alan ce": "${b ala nce s. su m} "} " " Code source sur Github " } https://github.com/fsarradin/bankapp.git 27 au 29 mars 2013
  21. 21. Type monadique Try Future (exception) (asynchrone) Contexte Monade Aspect technique Pureté fonctionnelle Option List Reader ... (absence) (non-déterminisme) (depend. injection)
  22. 22. flatMap : opération monadiquefor { m.map(x => x <- m x + 1} )yield x + 1for { m1.flatMap(x => x <- m1 m2.map(y => y <- m2 x + y} )yield x + y )
  23. 23. def balanceByBank: String = { val balancesByBankJson: Iterable[String] = for ((bankName, accountNumbers <- ownerAccounts) yield { val balances: List[Double] = for (accountNumber <- accountNumbers.toList) yield getAccount(bankName, accountNumber ).balance Démonstration s"""{"name":"$bankNam Java 8 et les monades e","b alan ce": "${b ala nce s. su m} "} " " Code source sur Github " } https://github.com/fsarradin/bankapp-java.git 27 au 29 mars 2013
  24. 24. Conclusion ● Code métier ○Peu de changement ● Système de type ○Aspect technique (déclaratif) ○Composition métier/technique => code ○Validation => compilateur
  25. 25. Question ?

×