From c4057ef563171d12814bc8f8ea6c2d612bab9a01 Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 19:42:16 +0400 Subject: [PATCH 1/7] Add groupByNelMap method to List syntax --- core/src/main/scala/cats/syntax/list.scala | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index 70bbbe3462..399e97fe50 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -98,6 +98,34 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { ) } + /** + * Groups elements inside this [[List]] according to the [[Order]] + * of the keys produced by the given key function. + * And each element in a group is transformed into a value of type `B` + * using the mapping function. + * + * {{{ + * scala> import scala.collection.immutable.SortedMap + * scala> import cats.data.NonEmptyList + * scala> import cats.syntax.all._ + * scala> val list = List(12, -2, 3, -5) + * scala> val expectedResult = SortedMap(false -> NonEmptyList.of("-2", "-5"), true -> NonEmptyList.of("12", "3")) + * scala> val result = list.groupByNelMap(_ >= 0)(_.toString) + * scala> result === expectedResult + * res0: Boolean = true + * }}} + */ + def groupByNelMap[K, B](key: A => K, f: A => B)(implicit K: Order[K]): SortedMap[K, NonEmptyList[B]] = { + implicit val ordering: Ordering[K] = K.toOrdering + + toNel match { + case None => + SortedMap.empty[K, NonEmptyList[B]] + case Some(nel) => + nel.groupMap(key)(f) + } + } + /** * Produces a `NonEmptyList` containing cumulative results of applying the * operator going left to right. From f24fb460447600212452a4e2e0ce0cbc1b64790a Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 19:45:12 +0400 Subject: [PATCH 2/7] Use assertEquals in ListSuite --- tests/shared/src/test/scala/cats/tests/ListSuite.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/shared/src/test/scala/cats/tests/ListSuite.scala b/tests/shared/src/test/scala/cats/tests/ListSuite.scala index 8bb06f2d22..f7cbe63c39 100644 --- a/tests/shared/src/test/scala/cats/tests/ListSuite.scala +++ b/tests/shared/src/test/scala/cats/tests/ListSuite.scala @@ -76,7 +76,7 @@ class ListSuite extends CatsSuite { ) test("toNel on empty list returns None") { - assert(List.empty[Int].toNel === None) + assertEquals(List.empty[Int].toNel, None) } test("groupByNel should be consistent with groupBy")( @@ -104,8 +104,8 @@ class ListSuite extends CatsSuite { ) test("show") { - assert(List(1, 2, 3).show === "List(1, 2, 3)") - assert((Nil: List[Int]).show === "List()") + assertEquals(List(1, 2, 3).show, "List(1, 2, 3)") + assertEquals((Nil: List[Int]).show, "List()") forAll { (l: List[String]) => assert(l.show === (l.toString)) } @@ -118,7 +118,7 @@ class ListSuite extends CatsSuite { .apply() .sum - assert(sumAll == lst.sum) + assertEquals(sumAll, lst.sum) } } From 4719d61f16737f1943dca4e9f3dcee23608ec278 Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 19:53:07 +0400 Subject: [PATCH 3/7] Add test for groupByNelMap --- .../shared/src/test/scala/cats/tests/ListSuite.scala | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/shared/src/test/scala/cats/tests/ListSuite.scala b/tests/shared/src/test/scala/cats/tests/ListSuite.scala index f7cbe63c39..c815b9952c 100644 --- a/tests/shared/src/test/scala/cats/tests/ListSuite.scala +++ b/tests/shared/src/test/scala/cats/tests/ListSuite.scala @@ -71,7 +71,7 @@ class ListSuite extends CatsSuite { test("nel => list => nel returns original nel")( forAll { (fa: NonEmptyList[Int]) => - assert(fa.toList.toNel === (Some(fa))) + assert(fa.toList.toNel === Some(fa)) } ) @@ -85,9 +85,15 @@ class ListSuite extends CatsSuite { } ) + test("groupByNelMap should be consistent with groupBy + map")( + forAll { (fa: List[Int], f: Int => Int, g: Int => Int) => + assert((fa.groupByNelMap(f, g).map { case (k, v) => (k, v.toList) }: Map[Int, List[Int]]) === fa.groupBy(f).mapValues(_.map(g))) + } + ) + test("groupByNelA should be consistent with groupByNel")( forAll { (fa: List[Int], f: Int => Int) => - assert(fa.groupByNelA(f.andThen(Option(_))) === (Option(fa.groupByNel(f)))) + assert(fa.groupByNelA(f.andThen(Option(_))) === Option(fa.groupByNel(f))) } ) @@ -107,7 +113,7 @@ class ListSuite extends CatsSuite { assertEquals(List(1, 2, 3).show, "List(1, 2, 3)") assertEquals((Nil: List[Int]).show, "List()") forAll { (l: List[String]) => - assert(l.show === (l.toString)) + assert(l.show === l.toString) } } From 48b012e3bb013f04bb4a2b7d02a08178c3b93004 Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 19:55:20 +0400 Subject: [PATCH 4/7] Scalafmt --- tests/shared/src/test/scala/cats/tests/ListSuite.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/shared/src/test/scala/cats/tests/ListSuite.scala b/tests/shared/src/test/scala/cats/tests/ListSuite.scala index c815b9952c..dd28f2bc05 100644 --- a/tests/shared/src/test/scala/cats/tests/ListSuite.scala +++ b/tests/shared/src/test/scala/cats/tests/ListSuite.scala @@ -87,7 +87,11 @@ class ListSuite extends CatsSuite { test("groupByNelMap should be consistent with groupBy + map")( forAll { (fa: List[Int], f: Int => Int, g: Int => Int) => - assert((fa.groupByNelMap(f, g).map { case (k, v) => (k, v.toList) }: Map[Int, List[Int]]) === fa.groupBy(f).mapValues(_.map(g))) + assert( + (fa.groupByNelMap(f, g).map { case (k, v) => (k, v.toList) }: Map[Int, List[Int]]) === fa + .groupBy(f) + .mapValues(_.map(g)) + ) } ) From 407b3c7f387916e5148929db7bfbe5a0fab9c0b8 Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 20:00:34 +0400 Subject: [PATCH 5/7] Fix scaladoc --- core/src/main/scala/cats/syntax/list.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index 399e97fe50..912b983b30 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -110,7 +110,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * scala> import cats.syntax.all._ * scala> val list = List(12, -2, 3, -5) * scala> val expectedResult = SortedMap(false -> NonEmptyList.of("-2", "-5"), true -> NonEmptyList.of("12", "3")) - * scala> val result = list.groupByNelMap(_ >= 0)(_.toString) + * scala> val result = list.groupByNelMap(_ >= 0, _.toString) * scala> result === expectedResult * res0: Boolean = true * }}} From fc1983587905360519574b8555c360daa9db75ee Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 20:05:42 +0400 Subject: [PATCH 6/7] Replace 'cats.implicits._' import in scaladocs --- core/src/main/scala/cats/syntax/list.scala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index 912b983b30..70cbf9d467 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -37,7 +37,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * Example: * {{{ * scala> import cats.data.NonEmptyList - * scala> import cats.implicits._ + * scala> import cats.syntax.list_ * * scala> val result1: List[Int] = List(1, 2) * scala> result1.toNel @@ -57,7 +57,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * {{{ * scala> import cats.data.NonEmptyList * scala> import scala.collection.immutable.SortedMap - * scala> import cats.implicits._ + * scala> import cats.syntax.all._ * * scala> val list = List(12, -2, 3, -5) * @@ -79,7 +79,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * {{{ * scala> import cats.data.NonEmptyList * scala> import scala.collection.immutable.SortedMap - * scala> import cats.implicits._ + * scala> import cats.syntax.all._ * * scala> val list = List(12, -2, 3, -5) * @@ -108,6 +108,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * scala> import scala.collection.immutable.SortedMap * scala> import cats.data.NonEmptyList * scala> import cats.syntax.all._ + * * scala> val list = List(12, -2, 3, -5) * scala> val expectedResult = SortedMap(false -> NonEmptyList.of("-2", "-5"), true -> NonEmptyList.of("12", "3")) * scala> val result = list.groupByNelMap(_ >= 0, _.toString) @@ -133,7 +134,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * Example: * {{{ * scala> import cats.data.NonEmptyList - * scala> import cats.implicits._ + * scala> import cats.syntax.all._ * * scala> val result1: List[Int] = List(1, 2) * scala> result1.scanLeftNel(100)(_ + _) @@ -154,7 +155,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * Example: * {{{ * scala> import cats.data.NonEmptyList - * scala> import cats.implicits._ + * scala> import cats.syntax.all._ * * scala> val result1: List[Int] = List(1, 2) * scala> result1.scanRightNel(100)(_ + _) @@ -182,7 +183,7 @@ final private[syntax] class ListOpsBinCompat0[A](private val la: List[A]) extend * {{{ * scala> import cats.data.NonEmptyChain * scala> import scala.collection.immutable.SortedMap - * scala> import cats.implicits._ + * scala> import cats.syntax.all._ * * scala> val list = List(12, -2, 3, -5) * From 9d0d7cdfff6564d1a8766f941d54599b2e050b8b Mon Sep 17 00:00:00 2001 From: danicheg Date: Mon, 30 May 2022 20:11:26 +0400 Subject: [PATCH 7/7] Fix test --- core/src/main/scala/cats/syntax/list.scala | 2 +- tests/shared/src/test/scala/cats/tests/ListSuite.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index 70cbf9d467..e8f07038bb 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -37,7 +37,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * Example: * {{{ * scala> import cats.data.NonEmptyList - * scala> import cats.syntax.list_ + * scala> import cats.syntax.list._ * * scala> val result1: List[Int] = List(1, 2) * scala> result1.toNel diff --git a/tests/shared/src/test/scala/cats/tests/ListSuite.scala b/tests/shared/src/test/scala/cats/tests/ListSuite.scala index dd28f2bc05..318a9c01df 100644 --- a/tests/shared/src/test/scala/cats/tests/ListSuite.scala +++ b/tests/shared/src/test/scala/cats/tests/ListSuite.scala @@ -90,7 +90,7 @@ class ListSuite extends CatsSuite { assert( (fa.groupByNelMap(f, g).map { case (k, v) => (k, v.toList) }: Map[Int, List[Int]]) === fa .groupBy(f) - .mapValues(_.map(g)) + .map { case (k, v) => (k, v.map(g)) } ) } )