Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core-tests/shared/src/test/scala/zio/stm/TSetSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ object TSetSpec extends ZIOBaseSpec {
testM("apply") {
val tx = TSet.make(1, 2, 2, 3).flatMap[Any, Nothing, List[Int]](_.toList)
assertM(tx.commit)(hasSameElements(List(1, 2, 3)))

},
testM("empty") {
val tx = TSet.empty[Int].flatMap[Any, Nothing, List[Int]](_.toList)
Expand Down Expand Up @@ -200,6 +199,17 @@ object TSetSpec extends ZIOBaseSpec {
} yield res

assertM(tx.commit)(equalTo(0))
},
testM("toSet") {
val set = Set(1, 2, 3)

val tx =
for {
tset <- TSet.fromIterable(set)
res <- tset.toSet
} yield res

assertM(tx.commit)(hasSameElements(set))
}
),
suite("set operations")(
Expand Down
9 changes: 7 additions & 2 deletions core/shared/src/main/scala/zio/stm/TSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class TSet[A] private (private val tmap: TMap[A, Unit]) extends AnyVal {
* provided set.
*/
def diff(other: TSet[A]): USTM[Unit] =
other.toList.map(_.toSet).flatMap(vals => removeIf(vals.contains))
other.toSet.flatMap(vals => removeIf(vals.contains))

/**
* Atomically folds using a pure function.
Expand All @@ -69,7 +69,7 @@ final class TSet[A] private (private val tmap: TMap[A, Unit]) extends AnyVal {
* provided set.
*/
def intersect(other: TSet[A]): USTM[Unit] =
other.toList.map(_.toSet).flatMap(vals => retainIf(vals.contains))
other.toSet.flatMap(vals => retainIf(vals.contains))

/**
* Stores new element in the set.
Expand Down Expand Up @@ -99,6 +99,11 @@ final class TSet[A] private (private val tmap: TMap[A, Unit]) extends AnyVal {
*/
def toList: USTM[List[A]] = tmap.keys

/**
* Collects all elements into a set.
*/
def toSet: USTM[Set[A]] = toList.map(_.toSet)

/**
* Atomically updates all elements using a pure function.
*/
Expand Down