Skip to content

Commit

Permalink
Add NonEmptyMap#toNonEmptyList (issue #2346)
Browse files Browse the repository at this point in the history
  • Loading branch information
CucumisSativus committed Sep 25, 2018
1 parent 018cb0f commit 1daf808
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyMapImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit 1daf808

Please sign in to comment.