SlideShare a Scribd company logo
Pour quelques monades
de plus...
Apprendre Haskell vous fera
le plus grand bien !
http://lyah.haskell.fr/
Un DSL pour ma base de
données
exemple avec Aerospike
Pure function
X Y
Pure function
val increment: Int => Int = x => x + 1
Impure function
X Y
External world
Impure function
val get: String => String = url => httpClient.get(url)
Real World
● La pureté c’est bien…
● … Mais dans le monde réel, c’est possible?
Real World
● La pureté c’est bien…
● … Mais dans une vraie app, c’est possible?
IO monad
Impure
X IO[Y]
External world
IO
Future[T]
● Approche concurrente
● Asynchrone
● Future[T] : Contient potentiellement une valeur T
val httpClient: HttpClient = ???
def getAsync(url: String): Future[String] = {
val promise = Promise[String]()
httpClient.get(url, AsyncHandler {
content => promise.success(content),
error => promise.failure(error)
})
promise.future
}
Future[T]
Future est une monade IO?
Future[T]
Future est une monade IO?
Referential transparency
val future: Future[A] = ???
!=
def future: Future[A] = ???
Scala IO library
cats-effect Monix
scalaz-effect
IO with Scala
● Confiner les effets de bords
val div: IO[Unit] = IO {
val a = io.StdIn.readInt()
val b = io.StdIn.readInt()
if (b == 0) throw new IllegalArgumentException()
else println(a / b)
}
div.runUnsafeSync //execute in real world
Real world app : Aerospike
● clé -> valeur
● In memory / SSD
● Clustering
IO with Aerospike
def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb =>
val listener = new WriteListener {
override def onFailure(exception: AerospikeException): Unit = cb(Left(exception))
override def onSuccess(key: Key): Unit = cb(Right(()))
}
client.put(key, listener, bins)
}
val program: IO[Unit] = put(key, value)(client)
program.runUnsafeSync //execute in real world
IO with Aerospike
def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb =>
val listener = new WriteListener {
override def onFailure(exception: AerospikeException): Unit = cb(Left(exception))
override def onSuccess(key: Key): Unit = cb(Right(()))
}
client.put(key, listener, bins)
}
val program: IO[Unit] = put(key, value)(client)
program.runUnsafeSync //execute in real world
IO with Aerospike
def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb =>
val listener = new WriteListener {
override def onFailure(exception: AerospikeException): Unit = cb(Left(exception))
override def onSuccess(key: Key): Unit = cb(Right(()))
}
client.put(key, listener, bins)
}
val program: IO[Unit] = put(key, value)(client)
program.runUnsafeSync //execute in real world
AerospikeIO[T]
● Réduire le boilerplate
● Description de l’interaction
● Pas de code technique
● Monade IO spécifique
Algèbre et interpréteur
Algèbre (monadique)
sealed trait AerospikeIO[A]
case class Get[A](key: Key) extends AerospikeIO[A]
case class Put[A](key: Key, value: A) extends AerospikeIO[Unit]
case class Pure[A](x: A) extends AerospikeIO[A]
case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends
AerospikeIO[(A, B)]
case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends
AerospikeIO[B]
Algèbre (monadique)
sealed trait AerospikeIO[A]
case class Get[A](key: Key) extends AerospikeIO[A]
case class Put[A](key: Key, value: A) extends AerospikeIO[Unit]
case class Pure[A](x: A) extends AerospikeIO[A]
case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends
AerospikeIO[(A, B)]
case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends
AerospikeIO[B]
Algèbre (monadique)
sealed trait AerospikeIO[A]
case class Get[A](key: Key) extends AerospikeIO[A]
case class Put[A](key: Key, value: A) extends AerospikeIO[Unit]
case class Pure[A](x: A) extends AerospikeIO[A]
case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends
AerospikeIO[(A, B)]
case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends
AerospikeIO[B]
Operations
case class Mark(name: String, value: Int)
val program: AerospikeIO[Mark] = for {
_ <- Put(keyBob, Mark("Bob", 17))
bobMark <- Get[Mark](keyBob)
} yield bobMark
Operations (Représentation)
FlatMap[Mark](
Put(keyBob, Mark(“Bob”, 17),
_ => Get[Mark](keyBob)
)
Interpréteur : Transformation Naturelle
val nt = new (List ~> Option) {
def apply[A](list: List[A]): Option[A] = list match {
case Nil => None
case x :: xs => Some(x)
}
}
//nt(1 :: Nil) => Some(1)
Interpréteur : Kleisli
val keepPositiveValues = (i: Int) => if (i > 0) Some(i) else None
val lowerThanTen = (i: Int) => if (i < 10) Some(i) else None
keepPositiveValues.andThen(lowerThanTen)
Interpréteur : Kleisli
val keepPositiveValues = (i: Int) => if (i > 0) Some(i) else None
val lowerThanTen = (i: Int) => if (i < 10) Some(i) else None
keepPositiveValues.andThen(lowerThanTen) // doesn't compile!
Interpréteur : Kleisli
-1
Some[Int]
keepPositiveValue(-1)
None
Extrait la valeur de
Option[Int]
Interpréteur : Kleisli
val keepPositiveValues = Kleisli[Option, Int, Int] { i =>
if (i > 0) Some(i) else None
}
val lowerThanTen = Kleisli[Option, Int, Int] { i =>
if (i < 10) Some(i) else None
}
keepPositiveValues.andThen(lowerThanTen) // compile
Interpréteur
type Stack[A] = Kleisli[Future, AerospikeClient, A]
~
AerospikeClient => Future[A]
Interpréteur
AerospikeIO ~> Stack
Interpréteur
case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client =>
val promise = Promise[Unit]
val listener = new WriteListener {
def onFailure(exception) = promise.failure(exception)
def onSuccess(key: Key)= promise.success(())
}
client.put(key, listener, bins)
promise.future
}
Interpréteur
case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client =>
val promise = Promise[Unit]
val listener = new WriteListener {
def onFailure(exception) = promise.failure(exception)
def onSuccess(key: Key)= promise.success(())
}
client.put(key, listener, bins)
promise.future
}
Interpréteur
case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client =>
val promise = Promise[Unit]
val listener = new WriteListener {
def onFailure(exception) = promise.failure(exception)
def onSuccess(key: Key)= promise.success(())
}
client.put(key, listener, bins)
promise.future
}
Interpréteur
case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client =>
val promise = Promise[Unit]
val listener = new WriteListener {
def onFailure(exception) = promise.failure(exception)
def onSuccess(key: Key)= promise.success(())
}
client.put(key, listener, bins)
promise.future
}
Run program
val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ???
case class Mark(name: String, value: Int)
val program: Aerospike[Mark] = for {
_ <- Put(keyBob, Mark("Bob", 17))
bobMark <- Get[Mark](keyBob)
} yield bobMark
val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program)
val result: Future[Mark] = kleisli(client)
Run program
val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ???
case class Mark(name: String, value: Int)
val program: Aerospike[Mark] = for {
_ <- Put(keyBob, Mark("Bob", 17))
bobMark <- Get[Mark](keyBob)
} yield bobMark
val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program)
val result: Future[Mark] = kleisli(client)
Run program
val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ???
case class Mark(name: String, value: Int)
val program: Aerospike[Mark] = for {
_ <- Put(keyBob, Mark("Bob", 17))
bobMark <- Get[Mark](keyBob)
} yield bobMark
val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program)
val result: Future[Mark] = kleisli(client)
Run program
val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ???
case class Mark(name: String, value: Int)
val program: Aerospike[Mark] = for {
_ <- Put(keyBob, Mark("Bob", 17))
bobMark <- Get[Mark](keyBob)
} yield bobMark
val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program)
val result: Future[Mark] = kleisli(client)
Sources
https://github.com/travisbrown/circe-algebra
https://github.com/jdegoes/scalaworld-2015
https://github.com/tpolecat/doobie
Merci!
https://github.com/tabmo/aerospike4s
https://github.com/rlecomte/presentation-fug-mtp
@lebalifant
@TabMoLabs

More Related Content

What's hot

Berlin meetup
Berlin meetupBerlin meetup
Berlin meetup
Wiem Zine Elabidine
 
The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185
Mahmoud Samir Fayed
 
ECMAScript 5: Новое в JavaScript
ECMAScript 5: Новое в JavaScriptECMAScript 5: Новое в JavaScript
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
John De Goes
 
Ray tracing with ZIO-ZLayer
Ray tracing with ZIO-ZLayerRay tracing with ZIO-ZLayer
Ray tracing with ZIO-ZLayer
Pierangelo Cecchetto
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
Noritada Shimizu
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
Wiem Zine Elabidine
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
Ray Tracing with ZIO
Ray Tracing with ZIORay Tracing with ZIO
Ray Tracing with ZIO
Pierangelo Cecchetto
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
Chris Ohk
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
Ahmad Arif Faizin
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
Christoph Bauer
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
jeffz
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 

What's hot (20)

Berlin meetup
Berlin meetupBerlin meetup
Berlin meetup
 
The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185
 
ECMAScript 5: Новое в JavaScript
ECMAScript 5: Новое в JavaScriptECMAScript 5: Новое в JavaScript
ECMAScript 5: Новое в JavaScript
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Ray tracing with ZIO-ZLayer
Ray tracing with ZIO-ZLayerRay tracing with ZIO-ZLayer
Ray tracing with ZIO-ZLayer
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Ray Tracing with ZIO
Ray Tracing with ZIORay Tracing with ZIO
Ray Tracing with ZIO
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
Struct examples
Struct examplesStruct examples
Struct examples
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similar to Un dsl pour ma base de données

Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
Siarhiej Siemianchuk
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
scalaconfjp
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
Richard Fox
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
Alexander Zaidel
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
Hang Zhao
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
Christian Baranowski
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
Oscar Renalias
 
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the CityGreyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
Natan Silnitsky
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
Eleanor McHugh
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type Classes
Tapio Rautonen
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
Marina Sigaeva
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
Adam L Barrett
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scalalunfu zhong
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
Fabernovel
 

Similar to Un dsl pour ma base de données (20)

Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
 
Monadologie
MonadologieMonadologie
Monadologie
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
 
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the CityGreyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
Greyhound - Powerful Pure Functional Kafka Library - Scala Love in the City
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type Classes
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 

Recently uploaded

Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 

Un dsl pour ma base de données

  • 2. Apprendre Haskell vous fera le plus grand bien ! http://lyah.haskell.fr/
  • 3. Un DSL pour ma base de données exemple avec Aerospike
  • 5. Pure function val increment: Int => Int = x => x + 1
  • 7. Impure function val get: String => String = url => httpClient.get(url)
  • 8. Real World ● La pureté c’est bien… ● … Mais dans le monde réel, c’est possible?
  • 9. Real World ● La pureté c’est bien… ● … Mais dans une vraie app, c’est possible?
  • 11. Future[T] ● Approche concurrente ● Asynchrone ● Future[T] : Contient potentiellement une valeur T val httpClient: HttpClient = ??? def getAsync(url: String): Future[String] = { val promise = Promise[String]() httpClient.get(url, AsyncHandler { content => promise.success(content), error => promise.failure(error) }) promise.future }
  • 14. Referential transparency val future: Future[A] = ??? != def future: Future[A] = ???
  • 15. Scala IO library cats-effect Monix scalaz-effect
  • 16. IO with Scala ● Confiner les effets de bords val div: IO[Unit] = IO { val a = io.StdIn.readInt() val b = io.StdIn.readInt() if (b == 0) throw new IllegalArgumentException() else println(a / b) } div.runUnsafeSync //execute in real world
  • 17. Real world app : Aerospike ● clé -> valeur ● In memory / SSD ● Clustering
  • 18. IO with Aerospike def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb => val listener = new WriteListener { override def onFailure(exception: AerospikeException): Unit = cb(Left(exception)) override def onSuccess(key: Key): Unit = cb(Right(())) } client.put(key, listener, bins) } val program: IO[Unit] = put(key, value)(client) program.runUnsafeSync //execute in real world
  • 19. IO with Aerospike def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb => val listener = new WriteListener { override def onFailure(exception: AerospikeException): Unit = cb(Left(exception)) override def onSuccess(key: Key): Unit = cb(Right(())) } client.put(key, listener, bins) } val program: IO[Unit] = put(key, value)(client) program.runUnsafeSync //execute in real world
  • 20. IO with Aerospike def put[A](key: Key, value: A)(client: AerospikeClient): IO[Unit] = IO.async { cb => val listener = new WriteListener { override def onFailure(exception: AerospikeException): Unit = cb(Left(exception)) override def onSuccess(key: Key): Unit = cb(Right(())) } client.put(key, listener, bins) } val program: IO[Unit] = put(key, value)(client) program.runUnsafeSync //execute in real world
  • 21. AerospikeIO[T] ● Réduire le boilerplate ● Description de l’interaction ● Pas de code technique ● Monade IO spécifique
  • 23. Algèbre (monadique) sealed trait AerospikeIO[A] case class Get[A](key: Key) extends AerospikeIO[A] case class Put[A](key: Key, value: A) extends AerospikeIO[Unit] case class Pure[A](x: A) extends AerospikeIO[A] case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends AerospikeIO[(A, B)] case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends AerospikeIO[B]
  • 24. Algèbre (monadique) sealed trait AerospikeIO[A] case class Get[A](key: Key) extends AerospikeIO[A] case class Put[A](key: Key, value: A) extends AerospikeIO[Unit] case class Pure[A](x: A) extends AerospikeIO[A] case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends AerospikeIO[(A, B)] case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends AerospikeIO[B]
  • 25. Algèbre (monadique) sealed trait AerospikeIO[A] case class Get[A](key: Key) extends AerospikeIO[A] case class Put[A](key: Key, value: A) extends AerospikeIO[Unit] case class Pure[A](x: A) extends AerospikeIO[A] case class Join[A, B](x: AerospikeIO[A], y: AerospikeIO[B]) extends AerospikeIO[(A, B)] case class FlatMap[A, B](x: AerospikeIO[A], f: A => AerospikeIO[B]) extends AerospikeIO[B]
  • 26. Operations case class Mark(name: String, value: Int) val program: AerospikeIO[Mark] = for { _ <- Put(keyBob, Mark("Bob", 17)) bobMark <- Get[Mark](keyBob) } yield bobMark
  • 28. Interpréteur : Transformation Naturelle val nt = new (List ~> Option) { def apply[A](list: List[A]): Option[A] = list match { case Nil => None case x :: xs => Some(x) } } //nt(1 :: Nil) => Some(1)
  • 29. Interpréteur : Kleisli val keepPositiveValues = (i: Int) => if (i > 0) Some(i) else None val lowerThanTen = (i: Int) => if (i < 10) Some(i) else None keepPositiveValues.andThen(lowerThanTen)
  • 30. Interpréteur : Kleisli val keepPositiveValues = (i: Int) => if (i > 0) Some(i) else None val lowerThanTen = (i: Int) => if (i < 10) Some(i) else None keepPositiveValues.andThen(lowerThanTen) // doesn't compile!
  • 32. Interpréteur : Kleisli val keepPositiveValues = Kleisli[Option, Int, Int] { i => if (i > 0) Some(i) else None } val lowerThanTen = Kleisli[Option, Int, Int] { i => if (i < 10) Some(i) else None } keepPositiveValues.andThen(lowerThanTen) // compile
  • 33. Interpréteur type Stack[A] = Kleisli[Future, AerospikeClient, A] ~ AerospikeClient => Future[A]
  • 35. Interpréteur case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client => val promise = Promise[Unit] val listener = new WriteListener { def onFailure(exception) = promise.failure(exception) def onSuccess(key: Key)= promise.success(()) } client.put(key, listener, bins) promise.future }
  • 36. Interpréteur case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client => val promise = Promise[Unit] val listener = new WriteListener { def onFailure(exception) = promise.failure(exception) def onSuccess(key: Key)= promise.success(()) } client.put(key, listener, bins) promise.future }
  • 37. Interpréteur case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client => val promise = Promise[Unit] val listener = new WriteListener { def onFailure(exception) = promise.failure(exception) def onSuccess(key: Key)= promise.success(()) } client.put(key, listener, bins) promise.future }
  • 38. Interpréteur case Put(key, bins) => Kleisli[Future, AerospikeClient, Unit] { client => val promise = Promise[Unit] val listener = new WriteListener { def onFailure(exception) = promise.failure(exception) def onSuccess(key: Key)= promise.success(()) } client.put(key, listener, bins) promise.future }
  • 39. Run program val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ??? case class Mark(name: String, value: Int) val program: Aerospike[Mark] = for { _ <- Put(keyBob, Mark("Bob", 17)) bobMark <- Get[Mark](keyBob) } yield bobMark val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program) val result: Future[Mark] = kleisli(client)
  • 40. Run program val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ??? case class Mark(name: String, value: Int) val program: Aerospike[Mark] = for { _ <- Put(keyBob, Mark("Bob", 17)) bobMark <- Get[Mark](keyBob) } yield bobMark val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program) val result: Future[Mark] = kleisli(client)
  • 41. Run program val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ??? case class Mark(name: String, value: Int) val program: Aerospike[Mark] = for { _ <- Put(keyBob, Mark("Bob", 17)) bobMark <- Get[Mark](keyBob) } yield bobMark val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program) val result: Future[Mark] = kleisli(client)
  • 42. Run program val interpreter: AerospikeIO ~> Kleisli[Future, AerospikeClient, ?] = ??? case class Mark(name: String, value: Int) val program: Aerospike[Mark] = for { _ <- Put(keyBob, Mark("Bob", 17)) bobMark <- Get[Mark](keyBob) } yield bobMark val kleisli: Kleisli[Future, AerospikeClient, Mark] = interpreter(program) val result: Future[Mark] = kleisli(client)