From fb6045688652df7d35f03d17eb8d414c3423aa5e Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sat, 7 Oct 2017 12:26:41 +0800 Subject: [PATCH 01/12] Example for beginners to use Arrow composition --- core/src/main/scala/cats/arrow/Compose.scala | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/src/main/scala/cats/arrow/Compose.scala b/core/src/main/scala/cats/arrow/Compose.scala index d5157f24eb..783c2a37d7 100644 --- a/core/src/main/scala/cats/arrow/Compose.scala +++ b/core/src/main/scala/cats/arrow/Compose.scala @@ -8,6 +8,19 @@ import simulacrum.typeclass */ @typeclass trait Compose[F[_, _]] { self => + /** + * Here's how you can use `>>>` and `<<<` + * Example: + * {{{ + * scala> import cats.implicits._ + * scala> import cats.arrow._ + * scala> val f = (_ + 1) + * scala> val g = (_ * 100) + * scala> (f >>> g)(3) + * res0: Int = 400 + * scala> (f <<< g)(3) + * res1: Int = 301 + * }}} @simulacrum.op("<<<", alias = true) def compose[A, B, C](f: F[B, C], g: F[A, B]): F[A, C] From 604b71ecb6cb3bab0d72b011b68c44cd7062d36f Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sat, 7 Oct 2017 12:29:37 +0800 Subject: [PATCH 02/12] Example for beginners to use Arrow composition --- core/src/main/scala/cats/arrow/Compose.scala | 25 ++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/core/src/main/scala/cats/arrow/Compose.scala b/core/src/main/scala/cats/arrow/Compose.scala index 783c2a37d7..71f7a08ebb 100644 --- a/core/src/main/scala/cats/arrow/Compose.scala +++ b/core/src/main/scala/cats/arrow/Compose.scala @@ -9,18 +9,19 @@ import simulacrum.typeclass @typeclass trait Compose[F[_, _]] { self => /** - * Here's how you can use `>>>` and `<<<` - * Example: - * {{{ - * scala> import cats.implicits._ - * scala> import cats.arrow._ - * scala> val f = (_ + 1) - * scala> val g = (_ * 100) - * scala> (f >>> g)(3) - * res0: Int = 400 - * scala> (f <<< g)(3) - * res1: Int = 301 - * }}} + * Here's how you can use `>>>` and `<<<` + * Example: + * {{{ + * scala> import cats.implicits._ + * scala> import cats.arrow._ + * scala> val f = (_ + 1) + * scala> val g = (_ * 100) + * scala> (f >>> g)(3) + * res0: Int = 400 + * scala> (f <<< g)(3) + * res1: Int = 301 + * }}} + */ @simulacrum.op("<<<", alias = true) def compose[A, B, C](f: F[B, C], g: F[A, B]): F[A, C] From 6f6b09e9b0cda13a900a5eeaf71bf64180dcfe8d Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sun, 8 Oct 2017 09:55:44 +0800 Subject: [PATCH 03/12] Got rid of whitespace at EOL --- core/src/main/scala/cats/arrow/Compose.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/arrow/Compose.scala b/core/src/main/scala/cats/arrow/Compose.scala index 71f7a08ebb..326fc6d949 100644 --- a/core/src/main/scala/cats/arrow/Compose.scala +++ b/core/src/main/scala/cats/arrow/Compose.scala @@ -8,7 +8,7 @@ import simulacrum.typeclass */ @typeclass trait Compose[F[_, _]] { self => - /** + /** * Here's how you can use `>>>` and `<<<` * Example: * {{{ From 7adc57da51ac74019f66b5dd72682768c3bf0098 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Mon, 9 Oct 2017 22:58:27 +0800 Subject: [PATCH 04/12] Added type signatures --- core/src/main/scala/cats/arrow/Compose.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/arrow/Compose.scala b/core/src/main/scala/cats/arrow/Compose.scala index 326fc6d949..e2db939f42 100644 --- a/core/src/main/scala/cats/arrow/Compose.scala +++ b/core/src/main/scala/cats/arrow/Compose.scala @@ -14,8 +14,8 @@ import simulacrum.typeclass * {{{ * scala> import cats.implicits._ * scala> import cats.arrow._ - * scala> val f = (_ + 1) - * scala> val g = (_ * 100) + * scala> val f : Int => Int = (_ + 1) + * scala> val g : Int => Int = (_ * 100) * scala> (f >>> g)(3) * res0: Int = 400 * scala> (f <<< g)(3) From bcbd847270d969cd0697eea95cee80b104743c87 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Tue, 10 Oct 2017 13:57:34 +0800 Subject: [PATCH 05/12] Got rid of the unused import that is borking the build --- core/src/main/scala/cats/arrow/Compose.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/scala/cats/arrow/Compose.scala b/core/src/main/scala/cats/arrow/Compose.scala index e2db939f42..f5e210332e 100644 --- a/core/src/main/scala/cats/arrow/Compose.scala +++ b/core/src/main/scala/cats/arrow/Compose.scala @@ -13,7 +13,6 @@ import simulacrum.typeclass * Example: * {{{ * scala> import cats.implicits._ - * scala> import cats.arrow._ * scala> val f : Int => Int = (_ + 1) * scala> val g : Int => Int = (_ * 100) * scala> (f >>> g)(3) From 782537f0813d401160cf7c14c1ba8df34425ba25 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Wed, 25 Oct 2017 10:46:50 +0800 Subject: [PATCH 06/12] Added examples for dimap,lmap,rmap for Cokleisli --- core/src/main/scala/cats/data/Cokleisli.scala | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index a21bf77216..0fafeaf42f 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -11,9 +11,31 @@ import scala.annotation.tailrec */ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => + /** + * Example: + * {{{ + * scala> import cats.Id, cats.implicits._ + * scala> val x : Id[Int] = 42 + * scala> def before(x: Int) = x + 1 + * scala> def after(x: Int) = x - 1 + * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) + * scala> example.dimap(before)(after) == 42 + * }}} + */ def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] = Cokleisli(fc => g(run(F.map(fc)(f)))) + /** + * Example: + * {{{ + * scala> import cats.Id, cats.implicits._ + * scala> val x : Id[Int] = 42 + * scala> def before(x: Int) = x + 1 + * scala> def after(x: Int) = x - 1 + * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) + * scala> example.lmap(before).rmap(after) == 42 + * }}} + */ def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] = Cokleisli(fc => run(F.map(fc)(f))) From 50dd65eacf376903e22f42c68e3a98f7a4da0940 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Fri, 27 Oct 2017 09:43:53 +0800 Subject: [PATCH 07/12] Exchanged the Cokleisli Id examples with NonEmptyList --- core/src/main/scala/cats/data/Cokleisli.scala | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index 0fafeaf42f..133a06db36 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -14,12 +14,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.implicits._ - * scala> val x : Id[Int] = 42 - * scala> def before(x: Int) = x + 1 - * scala> def after(x: Int) = x - 1 - * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) - * scala> example.dimap(before)(after) == 42 + * scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._ + * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) + * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * scala> def before(x: String) = x.toInt + * scala> def after(x: Int) = x.toString + * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) + * res0: String = 2 * }}} */ def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] = @@ -28,12 +29,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.implicits._ - * scala> val x : Id[Int] = 42 - * scala> def before(x: Int) = x + 1 - * scala> def after(x: Int) = x - 1 - * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) - * scala> example.lmap(before).rmap(after) == 42 + * scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._ + * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) + * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * scala> def before(x: String) = x.toInt + * scala> def after(x: Int) = x.toString + * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) + * res0: String = 2 * }}} */ def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] = @@ -42,6 +44,16 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => def map[C](f: B => C): Cokleisli[F, A, C] = Cokleisli(f compose run) + /** + * Example: + * {{{ + * scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _)) + * sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * + * scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3")) + * res4: Int = 6 + * }}} + */ def contramapValue[C](f: F[C] => F[A]): Cokleisli[F, C, B] = Cokleisli(run compose f) From e4396bfe9bdfbde370f59afe26750032715f046b Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Fri, 27 Oct 2017 09:45:42 +0800 Subject: [PATCH 08/12] Revert "Exchanged the Cokleisli Id examples with NonEmptyList" This reverts commit 50dd65eacf376903e22f42c68e3a98f7a4da0940. --- core/src/main/scala/cats/data/Cokleisli.scala | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index 133a06db36..0fafeaf42f 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -14,13 +14,12 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._ - * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() - * scala> def before(x: String) = x.toInt - * scala> def after(x: Int) = x.toString - * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) - * res0: String = 2 + * scala> import cats.Id, cats.implicits._ + * scala> val x : Id[Int] = 42 + * scala> def before(x: Int) = x + 1 + * scala> def after(x: Int) = x - 1 + * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) + * scala> example.dimap(before)(after) == 42 * }}} */ def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] = @@ -29,13 +28,12 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._ - * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() - * scala> def before(x: String) = x.toInt - * scala> def after(x: Int) = x.toString - * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) - * res0: String = 2 + * scala> import cats.Id, cats.implicits._ + * scala> val x : Id[Int] = 42 + * scala> def before(x: Int) = x + 1 + * scala> def after(x: Int) = x - 1 + * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) + * scala> example.lmap(before).rmap(after) == 42 * }}} */ def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] = @@ -44,16 +42,6 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => def map[C](f: B => C): Cokleisli[F, A, C] = Cokleisli(f compose run) - /** - * Example: - * {{{ - * scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _)) - * sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() - * - * scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3")) - * res4: Int = 6 - * }}} - */ def contramapValue[C](f: F[C] => F[A]): Cokleisli[F, C, B] = Cokleisli(run compose f) From 5401b183d7892d123c2509ef2061e0863cc85953 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Fri, 27 Oct 2017 10:54:02 +0800 Subject: [PATCH 09/12] Exchanged the Cokleisli examples from 'Id' to use 'NonEmptyList' instead --- core/src/main/scala/cats/data/Cokleisli.scala | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index 0fafeaf42f..20edcada9c 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -14,12 +14,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.implicits._ - * scala> val x : Id[Int] = 42 - * scala> def before(x: Int) = x + 1 - * scala> def after(x: Int) = x - 1 - * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) - * scala> example.dimap(before)(after) == 42 + * scala> import cats._, data._ + * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) + * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * scala> def before(x: String) = x.toInt + * scala> def after(x: Int) = x.toString + * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) + * res0: String = 2 * }}} */ def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] = @@ -28,12 +29,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => /** * Example: * {{{ - * scala> import cats.Id, cats.implicits._ - * scala> val x : Id[Int] = 42 - * scala> def before(x: Int) = x + 1 - * scala> def after(x: Int) = x - 1 - * scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) - * scala> example.lmap(before).rmap(after) == 42 + * scala> import cats._, data._, implicits._ + * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) + * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * scala> def before(x: String) = x.toInt + * scala> def after(x: Int) = x.toString + * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) + * res0: String = 2 * }}} */ def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] = @@ -42,6 +44,17 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => def map[C](f: B => C): Cokleisli[F, A, C] = Cokleisli(f compose run) + /** + * Example: + * {{{ + * scala> import cats._, data._ + * scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _)) + * sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() + * + * scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3")) + * res4: Int = 6 + * }}} + */ def contramapValue[C](f: F[C] => F[A]): Cokleisli[F, C, B] = Cokleisli(run compose f) From 6a70ceabe2e26fa88357e095a395ba0348516e63 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sat, 28 Oct 2017 10:12:38 +0800 Subject: [PATCH 10/12] Removed redundant renderings --- core/src/main/scala/cats/data/Cokleisli.scala | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index 20edcada9c..b311ed64d9 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -16,7 +16,6 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * {{{ * scala> import cats._, data._ * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() * scala> def before(x: String) = x.toInt * scala> def after(x: Int) = x.toString * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) @@ -31,7 +30,6 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * {{{ * scala> import cats._, data._, implicits._ * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() * scala> def before(x: String) = x.toInt * scala> def after(x: Int) = x.toString * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) @@ -49,7 +47,6 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * {{{ * scala> import cats._, data._ * scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _)) - * sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli() * * scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3")) * res4: Int = 6 From 517dceaa81a142b12586f38ca86da6fbd3bd7982 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sun, 29 Oct 2017 09:57:22 +0800 Subject: [PATCH 11/12] This is probably safer. --- core/src/main/scala/cats/data/Cokleisli.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index b311ed64d9..51310795a8 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -16,7 +16,7 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * {{{ * scala> import cats._, data._ * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * scala> def before(x: String) = x.toInt + * scala> def before(x: Double) = x.toInt * scala> def after(x: Int) = x.toString * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) * res0: String = 2 @@ -30,7 +30,7 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * {{{ * scala> import cats._, data._, implicits._ * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) - * scala> def before(x: String) = x.toInt + * scala> def before(x: Double) = x.toInt * scala> def after(x: Int) = x.toString * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) * res0: String = 2 From 3242791cfc6c9c18c2fca9a5208deb6d45a3e882 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Sun, 29 Oct 2017 10:02:57 +0800 Subject: [PATCH 12/12] doh.have not quite woke up yet. --- core/src/main/scala/cats/data/Cokleisli.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index 51310795a8..6dbf61e11d 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -18,7 +18,7 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) * scala> def before(x: Double) = x.toInt * scala> def after(x: Int) = x.toString - * scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) + * scala> f.dimap(before)(after).run(NonEmptyList.of(1.0,2.0)) * res0: String = 2 * }}} */ @@ -32,7 +32,7 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => * scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) * scala> def before(x: Double) = x.toInt * scala> def after(x: Int) = x.toString - * scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) + * scala> f.lmap(before).rmap(after).run(NonEmptyList.of(1.0,2.0)) * res0: String = 2 * }}} */