From 8b2083b9d19995bede4a07066f33093cd3714cc1 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Fri, 19 May 2017 10:59:27 +0900 Subject: [PATCH] 0.2.5-SNAPSHOT begins --- README.md | 29 +++++++++++-------- build.sbt | 2 +- .../src/main/scala/readme/DieselDemo.scala | 13 +++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1355682..7026f3f 100755 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To use diesel in your project, [add it to your build](#sbt). ## `@diesel` -The `@diesel` annotation that cuts out the boilerplate associated with writing composable Tagless Final DSLs. +The `@diesel` annotation cuts out the boilerplate associated with writing composable Tagless Final DSLs. The Dsl can be accessed directly from the companion object if you import a converter located in `ops` (customisable by passing a name to the annotation as an argument). This are useful when you need to compose multiple DSLs in the context of `F[_]`, but do not want to name all the interpreter parameters. @@ -23,7 +23,7 @@ Note that this can be used along-side `@ktrans`. ```scala import diesel._, cats._, cats.implicits._ -object DieselDemo { +object DieselDemo { // Declare your DSL @diesel @@ -39,34 +39,38 @@ object DieselDemo { // Import companion-to-interpreter aliasing sugar import Maths.ops._, Logger.ops._ + def prog[F[_]: Monad: Maths: Logger](x: Int, y: Int): F[Int] = { for { p <- Maths.times(x, y) _ <- Logger.info(s"Product: $p") s <- Maths.add(x, y) _ <- Logger.info(s"Sum: $s") - } yield p + s + f <- Maths.add(p, s) + _ <- Logger.info(s"Final: $s") + } yield f } def main(args: Array[String]): Unit = { - // Wire our interpreters in + // Wire in our interpreters implicit val mathsInterp = new Maths[Id] { def times(l: Int, r: Int) = l * r def add(l: Int, r: Int) = l + r } implicit val loggingInterp = new Logger[Id] { - def info(msg: String) = println(msg) + def info(msg: String) = println(msg) } - val _ = prog[Id](1, 2) + val _ = prog[Id](3, 4) } } /* [info] Running DieselDemo -Product: 2 -Sum: 3 +Product: 12 +Sum: 7 +Final: 7 */ ``` @@ -103,7 +107,7 @@ object Maths { def apply[F[_]](implicit m: Maths[F]): Maths[F] = m // In charge of aliasing your singleton Maths object to an in-scope Maths[F] :) - object op { + object ops { implicit def toDsl[F[_]](o: Maths.type)(implicit m: Maths[F]): Maths[F] = m } } @@ -113,9 +117,10 @@ object Maths { ## `@ktrans` -There is also a handy `@ktrans` annotation that adds a `transformK` method to a trait that is parameterised by a Kind that -takes 1 type parameter. It's useful when you want to transform any given implementation of that trait for `F[_]` into one - that implements it on `G[_]`. +The `@ktrans` annotation adds a `transformK` method to a trait that is parameterised by a Kind that takes 1 type parameter. +It's handy when you want to transform any given implementation of that trait on `F[_]` into one that implements it on `G[_]`. + +Useful because it saves you from having to write your boilerplate (method forwarding and results wrapping). Note that this can be used along-side `@diesel`. diff --git a/build.sbt b/build.sbt index 3eca648..85a043f 100755 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -lazy val theVersion = "0.2.4" +lazy val theVersion = "0.2.5-SNAPSHOT" lazy val theScalaVersion = "2.11.11" lazy val scalaVersions = Seq("2.11.11", "2.12.2") diff --git a/examples/src/main/scala/readme/DieselDemo.scala b/examples/src/main/scala/readme/DieselDemo.scala index c1aa083..57c417d 100644 --- a/examples/src/main/scala/readme/DieselDemo.scala +++ b/examples/src/main/scala/readme/DieselDemo.scala @@ -2,7 +2,7 @@ package readme import diesel._, cats._, cats.implicits._ -object DieselDemo { +object DieselDemo { // Declare your DSL @diesel @@ -18,13 +18,16 @@ object DieselDemo { // Import companion-to-interpreter aliasing sugar import Maths.ops._, Logger.ops._ + def prog[F[_]: Monad: Maths: Logger](x: Int, y: Int): F[Int] = { for { p <- Maths.times(x, y) _ <- Logger.info(s"Product: $p") s <- Maths.add(x, y) _ <- Logger.info(s"Sum: $s") - } yield p + s + f <- Maths.add(p, s) + _ <- Logger.info(s"Final: $s") + } yield f } def main(args: Array[String]): Unit = { @@ -35,10 +38,10 @@ object DieselDemo { def add(l: Int, r: Int) = l + r } implicit val loggingInterp = new Logger[Id] { - def info(msg: String) = println(msg) + def info(msg: String) = println(msg) } - val _ = prog[Id](1, 2) + val _ = prog[Id](3, 4) } -} \ No newline at end of file +}