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)) + } + } }