diff --git a/core/src/main/scala/cats/data/NonEmptyMapImpl.scala b/core/src/main/scala/cats/data/NonEmptyMapImpl.scala index b1efcb4c5d..9ce00f0ece 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 toNel: 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 e75f980f13..756f9c784d 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.toNel should ===(NonEmptyList.fromListUnsafe(nem.toSortedMap.toList)) + } + } }