@@ -31,6 +31,11 @@ object set extends SetInstances
31
31
32
32
@ suppressUnusedImportWarningForScalaVersionSpecific
33
33
trait SetInstances {
34
+ import SetInstances ._
35
+
36
+ // We use a def instead of val here as a workaround to the MiMa
37
+ // 'ReversedMissingMethodProblem' error.
38
+ implicit def alleycatsStdInstancesForSet
34
39
// Monad advertises parametricity, but Set relies on using
35
40
// universal hash codes and equality, which hurts our ability to
36
41
// rely on free theorems.
@@ -55,8 +60,31 @@ trait SetInstances {
55
60
// If we accept Monad for Set, we can also have Alternative, as
56
61
// Alternative only requires MonoidK (already accepted by cats-core) and
57
62
// the Applicative that comes from Monad.
58
- implicit val alleyCatsStdSetMonad : Monad [Set ] & Alternative [Set ] =
59
- new Monad [Set ] with Alternative [Set ] {
63
+ : Monad [Set ] & Alternative [Set ] & Traverse [Set ] & TraverseFilter [Set ] =
64
+ alleycatsStdInstancesForSet_
65
+
66
+ @ deprecated(" Use alleycatsStdInstancesForSet" , " 2.13.0" )
67
+ val alleyCatsSetTraverse : Traverse [Set ] = alleycatsStdInstancesForSet_
68
+ @ deprecated(" Use alleycatsStdInstancesForSet" , " 2.13.0" )
69
+ val alleyCatsStdSetMonad : Monad [Set ] & Alternative [Set ] = alleycatsStdInstancesForSet_
70
+ @ deprecated(" Use alleycatsStdInstancesForSet" , " 2.13.0" )
71
+ val alleyCatsSetTraverseFilter : TraverseFilter [Set ] = alleycatsStdInstancesForSet_
72
+ }
73
+
74
+ private [alleycats] object SetInstances {
75
+ private val alleycatsStdInstancesForSet_ : Monad [Set ] & Alternative [Set ] & Traverse [Set ] & TraverseFilter [Set ] =
76
+ new Monad [Set ] with Alternative [Set ] with Traverse [Set ] with TraverseFilter [Set ] {
77
+
78
+ // Since iteration order is not guaranteed for sets, folds and other
79
+ // traversals may produce different results for input sets which
80
+ // appear to be the same.
81
+ val traverse : Traverse [Set ] = this
82
+
83
+ def traverseFilter [G [_], A , B ](fa : Set [A ])(f : A => G [Option [B ]])(implicit G : Applicative [G ]): G [Set [B ]] =
84
+ traverse
85
+ .foldRight(fa, Eval .now(G .pure(Set .empty[B ])))((x, xse) => G .map2Eval(f(x), xse)((i, o) => i.fold(o)(o + _)))
86
+ .value
87
+
60
88
def pure [A ](a : A ): Set [A ] = Set (a)
61
89
override def map [A , B ](fa : Set [A ])(f : A => B ): Set [B ] = fa.map(f)
62
90
def flatMap [A , B ](fa : Set [A ])(f : A => Set [B ]): Set [B ] = fa.flatMap(f)
@@ -69,7 +97,7 @@ trait SetInstances {
69
97
if (fa.isEmpty) Eval .now(Set .empty[Z ]) // no need to evaluate fb
70
98
else fb.map(fb => map2(fa, fb)(f))
71
99
72
- def tailRecM [A , B ](a : A )(f : ( A ) => Set [Either [A , B ]]): Set [B ] = {
100
+ def tailRecM [A , B ](a : A )(f : A => Set [Either [A , B ]]): Set [B ] = {
73
101
val bldr = Set .newBuilder[B ]
74
102
75
103
@ tailrec def go (set : Set [Either [A , B ]]): Unit = {
@@ -98,13 +126,7 @@ trait SetInstances {
98
126
override def prependK [A ](a : A , fa : Set [A ]): Set [A ] = fa + a
99
127
100
128
override def appendK [A ](fa : Set [A ], a : A ): Set [A ] = fa + a
101
- }
102
129
103
- // Since iteration order is not guaranteed for sets, folds and other
104
- // traversals may produce different results for input sets which
105
- // appear to be the same.
106
- implicit val alleyCatsSetTraverse : Traverse [Set ] =
107
- new Traverse [Set ] {
108
130
def foldLeft [A , B ](fa : Set [A ], b : B )(f : (B , A ) => B ): B =
109
131
fa.foldLeft(b)(f)
110
132
def foldRight [A , B ](fa : Set [A ], lb : Eval [B ])(f : (A , Eval [B ]) => Eval [B ]): Eval [B ] =
@@ -172,14 +194,4 @@ trait SetInstances {
172
194
override def collectFirstSome [A , B ](fa : Set [A ])(f : A => Option [B ]): Option [B ] =
173
195
fa.collectFirst(Function .unlift(f))
174
196
}
175
-
176
- implicit val alleyCatsSetTraverseFilter : TraverseFilter [Set ] =
177
- new TraverseFilter [Set ] {
178
- val traverse : Traverse [Set ] = alleyCatsSetTraverse
179
-
180
- def traverseFilter [G [_], A , B ](fa : Set [A ])(f : A => G [Option [B ]])(implicit G : Applicative [G ]): G [Set [B ]] =
181
- traverse
182
- .foldRight(fa, Eval .now(G .pure(Set .empty[B ])))((x, xse) => G .map2Eval(f(x), xse)((i, o) => i.fold(o)(o + _)))
183
- .value
184
- }
185
197
}
0 commit comments