Skip to content

Commit

Permalink
Fixed #1312 - NonEmptySet#removeNonEmpty (#1313)
Browse files Browse the repository at this point in the history
* Fixed #1312 - NonEmptySet#removeNonEmpty

... and NonEmptySortedSet#removeNonEmpty

* Fixed #1312 - Formatting
  • Loading branch information
johnsonjii authored May 8, 2024
1 parent d70512f commit 053e4b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ object NonEmptySetSpec extends ZIOBaseSpec {
val expected = as.map(f).flatten
actual <-> expected
}
},
test("removeNonEmpty") {
check(genSet) { as =>
val toRemove = as.head
val actual = NonEmptySet.fromSetOption(as).get.removeNonEmpty(toRemove).map(_.toSet)
val modified = as - toRemove
val expected = Option(modified).filter(_.nonEmpty)
actual <-> expected
}
}
),
suite("constructors")(
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/prelude/NonEmptySet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class NonEmptySet[A] private (private val set: Set[A]) { self =>
*/
def removeNonEmpty(elem: A): Option[NonEmptySet[A]] = {
val newSet = set - elem
if (newSet.nonEmpty) Some(new NonEmptySet(set)) else None
if (newSet.nonEmpty) Some(new NonEmptySet(newSet)) else None
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class NonEmptySortedSet[A] private (private val set: SortedSet[A]) { self
*/
def removeNonEmpty(elem: A): Option[NonEmptySortedSet[A]] = {
val newSet = set - elem
if (newSet.nonEmpty) Some(new NonEmptySortedSet(set)) else None
if (newSet.nonEmpty) Some(new NonEmptySortedSet(newSet)) else None
}

/**
Expand Down

0 comments on commit 053e4b4

Please sign in to comment.