From d09412222d4ac3bc729af96fea00ffcb88a1b96b Mon Sep 17 00:00:00 2001 From: rfigueiredo Date: Wed, 20 May 2020 21:26:47 +0100 Subject: [PATCH 1/2] Add toNev to NonEmptyList variants --- .../scala-2.13+/cats/data/NonEmptyLazyList.scala | 15 +++++++++++++++ core/src/main/scala/cats/data/NonEmptyList.scala | 15 +++++++++++++++ .../cats/tests/NonEmptyLazyListSuite.scala | 6 ++++++ .../test/scala/cats/tests/NonEmptyListSuite.scala | 6 ++++++ 4 files changed, 42 insertions(+) diff --git a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala index 4e94ec5422..296dd70ea0 100644 --- a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala +++ b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala @@ -445,6 +445,21 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A]) final def toNes[B >: A](implicit order: Order[B]): NonEmptySet[B] = NonEmptySet.of(head, tail: _*) + /** + * Creates new `NonEmptyVector`, similarly to List#toVector from scala standard library. + *{{{ + * scala> import cats.data._ + * scala> import cats.instances.int._ + * scala> val nel = NonEmptyLazyList.fromLazyListPrepend(1, LazyList(2,3,4)) + * scala> val expectedResult = NonEmptyVector.fromVectorUnsafe(Vector(1,2,3,4)) + * scala> val result = nel.toNev + * scala> result === expectedResult + * res0: Boolean = true + *}}} + */ + final def toNev[B >: A]: NonEmptyVector[B] = + NonEmptyVector.fromVectorUnsafe(toLazyList.toList.toVector) + final def show[AA >: A](implicit AA: Show[AA]): String = s"NonEmpty${Show[LazyList[AA]].show(toLazyList)}" } diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index e6e8d48b2d..8c47757d11 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -437,6 +437,21 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec */ def toNes[B >: A](implicit order: Order[B]): NonEmptySet[B] = NonEmptySet.of(head, tail: _*) + + /** + * Creates new `NonEmptyVector`, similarly to List#toVector from scala standard library. + *{{{ + * scala> import cats.data._ + * scala> import cats.instances.int._ + * scala> val nel = NonEmptyList(1, List(2,3,4)) + * scala> val expectedResult = NonEmptyVector.fromVectorUnsafe(Vector(1,2,3,4)) + * scala> val result = nel.toNev + * scala> result === expectedResult + * res0: Boolean = true + *}}} + */ + def toNev[B >: A]: NonEmptyVector[B] = + NonEmptyVector.fromVectorUnsafe(toList.toVector) } object NonEmptyList extends NonEmptyListInstances { diff --git a/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala b/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala index 8a3894440b..9672c68902 100644 --- a/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala +++ b/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala @@ -142,6 +142,12 @@ class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLa ci.distinct.toList should ===(ci.toList.distinct) } } + + test("NonEmptyLazyList#toNev is consistent with List#toVector and creating NonEmptyVector from it") { + forAll { (ci: NonEmptyLazyList[Int]) => + ci.toNev should ===(NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ ci.toList.toVector)) + } + } } class ReducibleNonEmptyLazyListSuite extends ReducibleSuite[NonEmptyLazyList]("NonEmptyLazyList") { diff --git a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala index 7c7e3ff6b7..d30df5afc1 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala @@ -354,6 +354,12 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE nel.toNes should ===(NonEmptySet.fromSetUnsafe(SortedSet.empty[Int] ++ nel.toList.toSet)) } } + + test("NonEmptyList#toNev is consistent with List#toVector and creating NonEmptyVector from it") { + forAll { (nel: NonEmptyList[Int]) => + nel.toNev should ===(NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ nel.toList.toVector)) + } + } } @deprecated("to be able to test deprecated methods", since = "1.0.0-RC1") From cf92734b86c6d80368450c78aa2c8b1c13f0d403 Mon Sep 17 00:00:00 2001 From: rfigueiredo Date: Wed, 20 May 2020 22:40:22 +0100 Subject: [PATCH 2/2] Address comments. --- core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala | 2 +- .../src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala index 296dd70ea0..3ccfdb3017 100644 --- a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala +++ b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala @@ -458,7 +458,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A]) *}}} */ final def toNev[B >: A]: NonEmptyVector[B] = - NonEmptyVector.fromVectorUnsafe(toLazyList.toList.toVector) + NonEmptyVector.fromVectorUnsafe(toLazyList.toVector) final def show[AA >: A](implicit AA: Show[AA]): String = s"NonEmpty${Show[LazyList[AA]].show(toLazyList)}" } diff --git a/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala b/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala index 9672c68902..ac24021986 100644 --- a/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala +++ b/tests/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala @@ -1,7 +1,7 @@ package cats.tests import cats.{Align, Bimonad, SemigroupK, Show, Traverse} -import cats.data.{NonEmptyLazyList, NonEmptyLazyListOps} +import cats.data.{NonEmptyLazyList, NonEmptyLazyListOps, NonEmptyVector} import cats.kernel.{Eq, Hash, Order, PartialOrder, Semigroup} import cats.kernel.laws.discipline.{EqTests, HashTests, OrderTests, PartialOrderTests, SemigroupTests} import cats.laws.discipline.{AlignTests, BimonadTests, NonEmptyTraverseTests, SemigroupKTests, SerializableTests}