From 1daf80862016ae838ed7905355227fb51bf0a62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gutowski?= Date: Tue, 25 Sep 2018 21:50:00 +0200 Subject: [PATCH] Add NonEmptyMap#toNonEmptyList (issue #2346) --- core/src/main/scala/cats/data/NonEmptyMapImpl.scala | 4 ++++ tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/core/src/main/scala/cats/data/NonEmptyMapImpl.scala b/core/src/main/scala/cats/data/NonEmptyMapImpl.scala index b1efcb4c5d2..13292cca301 100644 --- a/core/src/main/scala/cats/data/NonEmptyMapImpl.scala +++ b/core/src/main/scala/cats/data/NonEmptyMapImpl.scala @@ -250,6 +250,10 @@ sealed class NonEmptyMapOps[K, A](val value: NonEmptyMap[K, A]) { */ def length: Int = toSortedMap.size + /** + * Returns a non empty list of map contents, similarly to Map#toList + */ + def toNonEmptyList: NonEmptyList[(K, A)] = NonEmptyList.fromListUnsafe(toSortedMap.toList) } private[data] sealed abstract class NonEmptyMapInstances { diff --git a/tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala index e75f980f13d..161345b001d 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala @@ -201,4 +201,10 @@ class NonEmptyMapSuite extends CatsSuite { nem.length should ===(nem.toSortedMap.size) } } + + test("NonEmptyMap#toNonEmptyList is consistent with Map#toList and creating NonEmptyList from it"){ + forAll{ nem: NonEmptyMap[String, Int] => + nem.toNonEmptyList should ===(NonEmptyList.fromListUnsafe(nem.toSortedMap.toList)) + } + } }