connection management fp style jed wesley smith jedws jed
play

Connection Management: FP Style! Jed Wesley-Smith @jedws Jed - PowerPoint PPT Presentation

Connection Management: FP Style! Jed Wesley-Smith @jedws Jed Wesley-Smith @jedws Jed Wesley-Smith @jedws Jed Wesley-Smith @jedws our problem our problem statistical analysis our problem statistical analysis heavy lifting in


  1. programs /** Apply the native function to the given arguments */ def native(name: String, args: Arg*): Program[REXP] = … def mean(x: Seq[Double]): Program[Double] = native("mean", x) flatMap toDouble def stdDev(x: Seq[Double]): Program[Double] = if (x.length < 2) const(0D) // < 2 datas will NaN else native("sd", x) flatMap toDouble

  2. programs /** Apply the native function to the given arguments */ def native(name: String, args: Arg*): Program[REXP] = … def mean(x: Seq[Double]): Program[Double] = native("mean", x) flatMap toDouble def stdDev(x: Seq[Double]): Program[Double] = if (x.length < 2) const(0D) // < 2 datas will NaN else native("sd", x) flatMap toDouble def meanAndStdDev(values: Seq[Double]) : Program[(Double, Double, Int)] =

  3. programs /** Apply the native function to the given arguments */ def native(name: String, args: Arg*): Program[REXP] = … def mean(x: Seq[Double]): Program[Double] = native("mean", x) flatMap toDouble def stdDev(x: Seq[Double]): Program[Double] = if (x.length < 2) const(0D) // < 2 datas will NaN else native("sd", x) flatMap toDouble def meanAndStdDev(values: Seq[Double]) : Program[(Double, Double, Int)] = for { m <- mean(values) sd <- stdDev(values) } yield (m, sd, values.length)

  4. now what?

  5. now what? • getting a value of type Program[A] is not the same as getting a value of type A, a program still needs to be executed.

  6. now what? • getting a value of type Program[A] is not the same as getting a value of type A, a program still needs to be executed. • how do we execute a program?

  7. now what? • getting a value of type Program[A] is not the same as getting a value of type A, a program still needs to be executed. • how do we execute a program? • first we need to compile it

  8. compile

  9. compile def compile[A](program: Program[A]): IO[A] =

  10. compile def compile[A](program: Program[A]): IO[A] = IO {

  11. compile def compile[A](program: Program[A]): IO[A] = IO { for { server <- Server.start()

  12. compile def compile[A](program: Program[A]): IO[A] = IO { for { server <- Server.start() result <- try server execute program

  13. compile def compile[A](program: Program[A]): IO[A] = IO { for { server <- Server.start() result <- try server execute program finally server.stop()

  14. compile def compile[A](program: Program[A]): IO[A] = IO { for { server <- Server.start() result <- try server execute program finally server.stop() } yield result }

  15. compile def compile[A](program: Program[A]): IO[A] = IO { for { server <- Server.start() result <- try server execute program finally server.stop() } yield result } def main(args: Array[String]) { batchedReduction.run.unsafePerformIO }

  16. pooled

  17. pooled def compilePooled[A](program: Program[A]): IO[A] =

  18. pooled def compilePooled[A](program: Program[A]): IO[A] = IO {

  19. pooled def compilePooled[A](program: Program[A]): IO[A] = IO { for { server <- pool.borrow

  20. pooled def compilePooled[A](program: Program[A]): IO[A] = IO { for { server <- pool.borrow result <- try server execute program finally pool + server

  21. pooled def compilePooled[A](program: Program[A]): IO[A] = IO { for { server <- pool.borrow result <- try server execute program finally pool + server } yield result }

  22. problems

  23. problems • error handling

  24. problems • error handling • tail recursion/stack overflow

  25. error handling

  26. error handling sealed trait Invalid

  27. error handling sealed trait Invalid case class Message(s: String) extends Invalid

  28. error handling sealed trait Invalid case class Message(s: String) extends Invalid case class Err(x: Throwable) extends Invalid { … // define custom eq as Throwable doesn’t define eq }

  29. error handling sealed trait Invalid case class Message(s: String) extends Invalid case class Err(x: Throwable) extends Invalid { … // define custom eq as Throwable doesn’t define eq } case class Composite(left: Invalid, right: Invalid) extends Invalid

Recommend


More recommend