Upload
francois-sarradin
View
497
Download
0
Embed Size (px)
DESCRIPTION
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.
Citation preview
11h30 - 12h20 - Auditorium
FLATMAP ZAT SHITLes monades expliquées aux geeks
27 au 29 mars 2013
FLATMAP ZAT SHITLes monades expliquées aux geeks
François SarradinDéveloppeur λ
@fsarradin
François Sarradin
● Développeur λ● http://kerflyn.wordpress.com/
● "Je suis un bagger"● http://www.brownbaglunch.fr/
@fsarradin
@bbl_fr
Entity x = getEntity1()
Entity y = getEntity2()
Entity z = x.get() + y.get()
Entity x = getEntity1()
Entity y = getEntity2()
Entity z = x.get() + y.get()
Asynchrone
null
Exception
Entity x = getEntity1()
Entity y = getEntity2()
Entity z = x.get() + y.get()
if (? == null)
try ...
finally
Thread
synchronized
...
if (? != null)
if (? !=
null)
try ... catch()
try ... catch()Thread
synchronized...
Entity x = getEntity1()
Entity y = getEntity2()
Entity z = x.get() + y.get()
* Conserve le code métier
Agenda
● Live coding / Scala 2.10○ Exception○ Asynchrone
● Code review / Java 8○ Exception
● Conclusion
Alice
InBank-land
Alice
Solde total ?
Service Web
Breizh BankLa PostaleBGP
BankService
Web
AccountRepository
getAccount(b, n)a:Account
JSON
Architecture
BankProxy BankProxy BankProxy
1: Get accounts
2: ???
3: PROFIT!
27 au 29 mars 2013
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 s"""{"name":"$bankNam e","b alan ce": "${b ala nce s. su m} "} " " " }
DémonstrationScala 2.10
Code source sur Githubhttps://github.com/fsarradin/bankapp.git
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)
1 Try[A] 1 for-expressionfor { 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)
2 Try[A] 1 for-expressionval 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 + yres1: scala.util.Try[Int] = Success(3)
for { x <- ko1; y <- ok2 } yield x + yres2: scala.util.Try[String] = Failure(Exception: #1)
for { x <- ko1; y <- ko2 } yield x + yres3: scala.util.Try[String] = Failure(Exception: #1)
27 au 29 mars 2013
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 s"""{"name":"$bankNam e","b alan ce": "${b ala nce s. su m} "} " " " }
DémonstrationScala 2.10 : Try
Code source sur Githubhttps://github.com/fsarradin/bankapp.git
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
Future[A] & for-expressionfor { 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
27 au 29 mars 2013
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 s"""{"name":"$bankNam e","b alan ce": "${b ala nce s. su m} "} " " " }
DémonstrationScala 2.10 : Future
Code source sur Githubhttps://github.com/fsarradin/bankapp.git
Try(exception)
Type monadique
Future(asynchrone)
Monade
Option(absence)
List(non-déterminisme)
Reader(depend. injection)
...
Contexte
Pureté fonctionnelleAspect technique
flatMap : opération monadique
for { x <- m}yield x + 1
for { x <- m1 y <- m2}yield x + y
m.map(x => x + 1)
m1.flatMap(x => m2.map(y => x + y ))
27 au 29 mars 2013
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 s"""{"name":"$bankNam e","b alan ce": "${b ala nce s. su m} "} " " " }
DémonstrationJava 8 et les monades
Code source sur Githubhttps://github.com/fsarradin/bankapp-java.git
Conclusion
● Code métier○ Peu de changement
● Système de type○ Aspect technique (déclaratif)○ Composition métier/technique => code○ Validation => compilateur
Question ?