Skip to content

Commit 97f53df

Browse files
author
Luka Jacobowitz
committed
Remove AnyVal and rename to toSortedSet
1 parent 8104b40 commit 97f53df

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

core/src/main/scala/cats/data/NonEmptySet.scala

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ import cats.{Always, Eq, Eval, Foldable, Later, Now, Reducible, SemigroupK, Show
2323

2424
import scala.collection.immutable._
2525

26-
final class NonEmptySet[A] private (val set: SortedSet[A]) extends AnyVal {
26+
final class NonEmptySet[A] private (val set: SortedSet[A]) {
2727

2828
private implicit def ordering: Ordering[A] = set.ordering
2929
private implicit def order: Order[A] = Order.fromOrdering
3030

3131
def +(a: A): NonEmptySet[A] = new NonEmptySet(set + a)
32-
def ++(as: NonEmptySet[A]): NonEmptySet[A] = concat(as)
32+
def ++(as: NonEmptySet[A]): NonEmptySet[A] = union(as)
33+
def |(as: NonEmptySet[A]): NonEmptySet[A] = union(as)
34+
def --(as: NonEmptySet[A]): SortedSet[A] = diff(as)
35+
def &~(as: NonEmptySet[A]): SortedSet[A] = diff(as)
36+
def &(as: NonEmptySet[A]): SortedSet[A] = intersect(as)
3337

34-
def concat(as: NonEmptySet[A]): NonEmptySet[A] = new NonEmptySet(set ++ as.set)
3538

3639
def -(a: A): SortedSet[A] = set - a
3740
def map[B: Order](f: A B): NonEmptySet[B] =
@@ -46,8 +49,9 @@ final class NonEmptySet[A] private (val set: SortedSet[A]) extends AnyVal {
4649
def apply(a: A): Boolean = set(a)
4750
def contains(a: A): Boolean = set.contains(a)
4851

49-
50-
def union(as: NonEmptySet[A]): NonEmptySet[A] = new NonEmptySet(set.union(as.toSet))
52+
def diff(as: NonEmptySet[A]): SortedSet[A] = set -- as.set
53+
def union(as: NonEmptySet[A]): NonEmptySet[A] = new NonEmptySet(set ++ as.set)
54+
def intersect(as: NonEmptySet[A]): SortedSet[A] = set.filter(as.apply)
5155
def size: Int = set.size
5256
def forall(p: A Boolean): Boolean = set.forall(p)
5357
def exists(f: A Boolean): Boolean = set.exists(f)
@@ -99,7 +103,7 @@ final class NonEmptySet[A] private (val set: SortedSet[A]) extends AnyVal {
99103
def reduce[AA >: A](implicit S: Semigroup[AA]): AA =
100104
S.combineAllOption(set).get
101105

102-
def toSet: SortedSet[A] = set
106+
def toSortedSet: SortedSet[A] = set
103107

104108
/**
105109
* Typesafe stringification method.
@@ -120,7 +124,7 @@ final class NonEmptySet[A] private (val set: SortedSet[A]) extends AnyVal {
120124
* universal equality provided by .equals.
121125
*/
122126
def ===(that: NonEmptySet[A]): Boolean =
123-
Eq[SortedSet[A]].eqv(set, that.toSet)
127+
Eq[SortedSet[A]].eqv(set, that.toSortedSet)
124128

125129
def length: Int = size
126130

@@ -139,7 +143,7 @@ final class NonEmptySet[A] private (val set: SortedSet[A]) extends AnyVal {
139143
* }}}
140144
*/
141145
def zipWith[B, C: Order](b: NonEmptySet[B])(f: (A, B) => C): NonEmptySet[C] =
142-
new NonEmptySet(SortedSet((set, b.toSet).zipped.map(f).to: _*)(Order[C].toOrdering))
146+
new NonEmptySet(SortedSet((set, b.toSortedSet).zipped.map(f).to: _*)(Order[C].toOrdering))
143147

144148
def zipWithIndex: NonEmptySet[(A, Int)] =
145149
new NonEmptySet(set.zipWithIndex)
@@ -150,7 +154,7 @@ private[data] sealed abstract class NonEmptySetInstances {
150154
new SemigroupK[NonEmptySet] with Reducible[NonEmptySet] {
151155

152156
def combineK[A](a: NonEmptySet[A], b: NonEmptySet[A]): NonEmptySet[A] =
153-
a ++ b
157+
a | b
154158

155159
override def size[A](fa: NonEmptySet[A]): Long = fa.length.toLong
156160

@@ -172,7 +176,7 @@ private[data] sealed abstract class NonEmptySetInstances {
172176
fa.foldRight(lb)(f)
173177

174178
override def foldMap[A, B](fa: NonEmptySet[A])(f: A => B)(implicit B: Monoid[B]): B =
175-
B.combineAll(fa.toSet.iterator.map(f))
179+
B.combineAll(fa.toSortedSet.iterator.map(f))
176180

177181
override def fold[A](fa: NonEmptySet[A])(implicit A: Monoid[A]): A =
178182
fa.reduce
@@ -186,7 +190,7 @@ private[data] sealed abstract class NonEmptySetInstances {
186190
override def exists[A](fa: NonEmptySet[A])(p: A => Boolean): Boolean =
187191
fa.exists(p)
188192

189-
override def toList[A](fa: NonEmptySet[A]): List[A] = fa.toSet.toList
193+
override def toList[A](fa: NonEmptySet[A]): List[A] = fa.toSortedSet.toList
190194

191195
override def toNonEmptyList[A](fa: NonEmptySet[A]): NonEmptyList[A] =
192196
NonEmptyList(fa.head, fa.tail.toList)
@@ -201,7 +205,7 @@ private[data] sealed abstract class NonEmptySetInstances {
201205
Show.show[NonEmptySet[A]](_.show)
202206

203207
implicit def catsDataSemilatticeForNonEmptySet[A]: Semilattice[NonEmptySet[A]] = new Semilattice[NonEmptySet[A]] {
204-
def combine(x: NonEmptySet[A], y: NonEmptySet[A]): NonEmptySet[A] = x ++ y
208+
def combine(x: NonEmptySet[A], y: NonEmptySet[A]): NonEmptySet[A] = x | y
205209
}
206210
}
207211

laws/src/main/scala/cats/laws/discipline/Arbitrary.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object arbitrary extends ArbitraryInstances0 {
5656
A.arbitrary.map(a => NonEmptySet(a, fa))))
5757

5858
implicit def catsLawsCogenForNonEmptySet[A: Order: Cogen]: Cogen[NonEmptySet[A]] =
59-
Cogen[SortedSet[A]].contramap(_.toSet)
59+
Cogen[SortedSet[A]].contramap(_.toSortedSet)
6060

6161
implicit def catsLawsArbitraryForZipVector[A](implicit A: Arbitrary[A]): Arbitrary[ZipVector[A]] =
6262
Arbitrary(implicitly[Arbitrary[Vector[A]]].arbitrary.map(v => new ZipVector(v)))

tests/src/test/scala/cats/tests/NonEmptySetSuite.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,56 +56,56 @@ class NonEmptySetSuite extends CatsSuite {
5656
forAll { (i: Int, tail: SortedSet[Int]) =>
5757
val set = tail + i
5858
val nonEmptySet = NonEmptySet(i, tail)
59-
set should === (nonEmptySet.toSet)
59+
set should === (nonEmptySet.toSortedSet)
6060
}
6161
}
6262

6363
test("NonEmptySet#filter is consistent with Set#filter") {
6464
forAll { (nes: NonEmptySet[Int], p: Int => Boolean) =>
65-
val set = nes.toSet
65+
val set = nes.toSortedSet
6666
nes.filter(p) should === (set.filter(p))
6767
}
6868
}
6969

7070
test("NonEmptySet#filterNot is consistent with Set#filterNot") {
7171
forAll { (nes: NonEmptySet[Int], p: Int => Boolean) =>
72-
val set = nes.toSet
72+
val set = nes.toSortedSet
7373
nes.filterNot(p) should === (set.filterNot(p))
7474
}
7575
}
7676

7777
test("NonEmptySet#collect is consistent with Set#collect") {
7878
forAll { (nes: NonEmptySet[Int], pf: PartialFunction[Int, String]) =>
79-
val set = nes.toSet
79+
val set = nes.toSortedSet
8080
nes.collect(pf) should === (set.collect(pf))
8181
}
8282
}
8383

8484
test("NonEmptySet#find is consistent with Set#find") {
8585
forAll { (nes: NonEmptySet[Int], p: Int => Boolean) =>
86-
val set = nes.toSet
86+
val set = nes.toSortedSet
8787
nes.find(p) should === (set.find(p))
8888
}
8989
}
9090

9191
test("NonEmptySet#exists is consistent with Set#exists") {
9292
forAll { (nes: NonEmptySet[Int], p: Int => Boolean) =>
93-
val set = nes.toSet
93+
val set = nes.toSortedSet
9494
nes.exists(p) should === (set.exists(p))
9595
}
9696
}
9797

9898
test("NonEmptySet#forall is consistent with Set#forall") {
9999
forAll { (nes: NonEmptySet[Int], p: Int => Boolean) =>
100-
val set = nes.toSet
100+
val set = nes.toSortedSet
101101
nes.forall(p) should === (set.forall(p))
102102
}
103103
}
104104

105105
test("NonEmptySet#map is consistent with Set#map") {
106106
forAll { (nes: NonEmptySet[Int], p: Int => String) =>
107-
val set = nes.toSet
108-
nes.map(p).toSet should === (set.map(p))
107+
val set = nes.toSortedSet
108+
nes.map(p).toSortedSet should === (set.map(p))
109109
}
110110
}
111111

@@ -177,17 +177,17 @@ class NonEmptySetSuite extends CatsSuite {
177177

178178
test("fromSet round trip") {
179179
forAll { l: SortedSet[Int] =>
180-
NonEmptySet.fromSet(l).map(_.toSet).getOrElse(SortedSet.empty[Int]) should === (l)
180+
NonEmptySet.fromSet(l).map(_.toSortedSet).getOrElse(SortedSet.empty[Int]) should === (l)
181181
}
182182

183183
forAll { nes: NonEmptySet[Int] =>
184-
NonEmptySet.fromSet(nes.toSet) should === (Some(nes))
184+
NonEmptySet.fromSet(nes.toSortedSet) should === (Some(nes))
185185
}
186186
}
187187

188188
test("fromSetUnsafe/fromSet consistency") {
189189
forAll { nes: NonEmptySet[Int] =>
190-
NonEmptySet.fromSet(nes.toSet) should === (Some(NonEmptySet.fromSetUnsafe(nes.toSet)))
190+
NonEmptySet.fromSet(nes.toSortedSet) should === (Some(NonEmptySet.fromSetUnsafe(nes.toSortedSet)))
191191
}
192192
}
193193

@@ -199,32 +199,32 @@ class NonEmptySetSuite extends CatsSuite {
199199

200200
test("+ consistent with Set") {
201201
forAll { (nes: NonEmptySet[Int], i: Int) =>
202-
(nes + i).toSet should === (nes.toSet + i)
202+
(nes + i).toSortedSet should === (nes.toSortedSet + i)
203203
}
204204
}
205205

206206
test("NonEmptySet#zipWithIndex is consistent with Set#zipWithIndex") {
207207
forAll { nes: NonEmptySet[Int] =>
208-
nes.zipWithIndex.toSet should === (nes.toSet.zipWithIndex)
208+
nes.zipWithIndex.toSortedSet should === (nes.toSortedSet.zipWithIndex)
209209
}
210210
}
211211

212212
test("NonEmptySet#size and length is consistent with Set#size") {
213213
forAll { nes: NonEmptySet[Int] =>
214-
nes.size should === (nes.toSet.size)
215-
nes.length should === (nes.toSet.size)
214+
nes.size should === (nes.toSortedSet.size)
215+
nes.length should === (nes.toSortedSet.size)
216216
}
217217
}
218218

219219
test("NonEmptySet#concat is consistent with Set#++") {
220220
forAll { (nes: NonEmptySet[Int], l: SortedSet[Int], n: Int) =>
221-
nes.concat(NonEmptySet(n, l)).toSet should === (nes.toSet ++ (l + n))
221+
nes.union(NonEmptySet(n, l)).toSortedSet should === (nes.toSortedSet ++ (l + n))
222222
}
223223
}
224224

225225
test("NonEmptySet#zipWith is consistent with Set#zip and then Set#map") {
226226
forAll { (a: NonEmptySet[Int], b: NonEmptySet[Int], f: (Int, Int) => Int) =>
227-
a.zipWith(b)(f).toSet should ===(a.toSet.zip(b.toSet).map { case (x, y) => f(x, y) })
227+
a.zipWith(b)(f).toSortedSet should ===(a.toSortedSet.zip(b.toSortedSet).map { case (x, y) => f(x, y) })
228228
}
229229
}
230230

0 commit comments

Comments
 (0)