From 8620303e98a1a9676395c75a29efc5fe40e83d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gutowski?= Date: Wed, 24 Oct 2018 15:42:54 +0200 Subject: [PATCH] Add NonEmptyList#toNes (issue #2346) (#2557) * Add NonEmptyList#toNes (issue #2346) * Fixed NonEmptyList.scala formating * Fixed NonEmptyListSuite.scala formating * Revert "Fixed NonEmptyListSuite.scala formating" This reverts commit 1a4ff0d7c24b697b0600784aaf0a6f1813bb61c3. * Fixed NonEmptyListSuite.scala formating, this time for sure --- core/src/main/scala/cats/data/NonEmptyList.scala | 13 +++++++++++++ .../test/scala/cats/tests/NonEmptyListSuite.scala | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index 0b7bb7d82b..2bb0d2842b 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -417,6 +417,19 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { */ def toNem[T, U](implicit ev: A <:< (T, U), order: Order[T]): NonEmptyMap[T, U] = NonEmptyMap.fromMapUnsafe(SortedMap(toList.map(ev): _*)(order.toOrdering)) + + /** + * Creates new `NonEmptySet`, similarly to List#toSet from scala standard library. + *{{{ + * scala> import cats.data._ + * scala> import cats.instances.int._ + * scala> val nel = NonEmptyList(1, List(2,2,3,4)) + * scala> nel.toNes + * res0: cats.data.NonEmptySet[Int] = TreeSet(1, 2, 3, 4) + *}}} + */ + def toNes[B >: A](implicit order: Order[B]): NonEmptySet[B] = + NonEmptySet.of(head, tail: _*) } object NonEmptyList extends NonEmptyListInstances { diff --git a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala index ed2b9a9ff3..e3d21e049c 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala @@ -3,7 +3,7 @@ package tests import cats.kernel.laws.discipline.{EqTests, OrderTests, PartialOrderTests, SemigroupTests} -import cats.data.{NonEmptyList, NonEmptyMap, NonEmptyVector} +import cats.data.{NonEmptyList, NonEmptyMap, NonEmptySet, NonEmptyVector} import cats.data.NonEmptyList.ZipNonEmptyList import cats.laws.discipline.arbitrary._ import cats.laws.discipline.{ @@ -15,6 +15,7 @@ import cats.laws.discipline.{ SerializableTests } import scala.collection.immutable.SortedMap +import scala.collection.immutable.SortedSet class NonEmptyListSuite extends CatsSuite { // Lots of collections here.. telling ScalaCheck to calm down a bit @@ -329,6 +330,12 @@ class NonEmptyListSuite extends CatsSuite { nel.toNem should ===(NonEmptyMap.fromMapUnsafe(SortedMap.empty[Int, String] ++ nel.toList.toMap)) } } + + test("NonEmptyList#toNes is consistent with List#toSet and creating NonEmptySet from it") { + forAll { nel: NonEmptyList[Int] => + nel.toNes should ===(NonEmptySet.fromSetUnsafe(SortedSet.empty[Int] ++ nel.toList.toSet)) + } + } } @deprecated("to be able to test deprecated methods", since = "1.0.0-RC1")