From 66d89573dd46c48db9da26e4b01a4237aa2063d3 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 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)) + } + } }