Skip to content

Commit 38fe343

Browse files
committed
Add support for collecting elements to a SortedSet or SortedMap
1 parent fdaae89 commit 38fe343

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

core/shared/src/main/scala-2.12/fs2/CollectorPlatform.scala

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import scala.collection.generic.{
2525
GenericTraversableTemplate,
2626
MapFactory,
2727
SetFactory,
28+
SortedMapFactory,
29+
SortedSetFactory,
2830
TraversableFactory
2931
}
30-
import scala.collection.{MapLike, SetLike, Traversable}
32+
import scala.collection.{MapLike, SetLike, SortedMapLike, SortedSetLike, Traversable}
3133

3234
import fs2.internal._
3335

@@ -52,11 +54,31 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
5254
): Collector.Aux[(K, V), C[K, V]] =
5355
make(Builder.fromMapFactory(f))
5456

57+
implicit def supportsSortedMapFactory[
58+
K: Ordering,
59+
V,
60+
C[a, b] <: collection.SortedMap[a, b] with SortedMapLike[
61+
a,
62+
b,
63+
C[a, b]
64+
]
65+
](
66+
f: SortedMapFactory[C]
67+
): Collector.Aux[(K, V), C[K, V]] =
68+
make(Builder.fromSortedMapFactory(f))
69+
5570
implicit def supportsSetFactory[A, C[x] <: Set[x] with SetLike[x, C[x]]](
5671
f: SetFactory[C]
5772
): Collector.Aux[A, C[A]] =
5873
make(Builder.fromSetFactory(f))
5974

75+
implicit def supportsSortedSetFactory[A: Ordering, C[x] <: collection.SortedSet[
76+
x
77+
] with SortedSetLike[x, C[x]]](
78+
f: SortedSetFactory[C]
79+
): Collector.Aux[A, C[A]] =
80+
make(Builder.fromSortedSetFactory(f))
81+
6082
private[fs2] trait BuilderPlatform { self: Collector.Builder.type =>
6183
def fromFactory[A, C[_], B](f: Factory[A, C[B]]): Builder[A, C[B]] =
6284
fromBuilder(f())
@@ -71,9 +93,25 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
7193
): Builder[(K, V), C[K, V]] =
7294
fromBuilder(f.newBuilder)
7395

96+
def fromSortedMapFactory[
97+
K: Ordering,
98+
V,
99+
C[a, b] <: collection.SortedMap[a, b] with SortedMapLike[a, b, C[a, b]]
100+
](
101+
f: SortedMapFactory[C]
102+
): Builder[(K, V), C[K, V]] =
103+
fromBuilder(f.newBuilder)
104+
74105
def fromSetFactory[A, C[x] <: collection.Set[x] with SetLike[x, C[x]]](
75106
f: SetFactory[C]
76107
): Builder[A, C[A]] =
77108
fromBuilder(f.newBuilder)
109+
110+
def fromSortedSetFactory[A: Ordering, C[x] <: collection.SortedSet[x] with SortedSetLike[x, C[
111+
x
112+
]]](
113+
f: SortedSetFactory[C]
114+
): Builder[A, C[A]] =
115+
fromBuilder(f.newBuilder)
78116
}
79117
}

core/shared/src/main/scala-2.13/fs2/CollectorPlatform.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
package fs2
2323

2424
import scala.collection.immutable.ArraySeq
25-
import scala.collection.{Factory, IterableFactory, MapFactory}
25+
import scala.collection.{
26+
Factory,
27+
EvidenceIterableFactory,
28+
IterableFactory,
29+
MapFactory,
30+
SortedMapFactory
31+
}
2632
import scala.reflect.ClassTag
2733

2834
private[fs2] trait CollectorPlatform { self: Collector.type =>
@@ -38,6 +44,16 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
3844
implicit def supportsMapFactory[K, V, C[_, _]](f: MapFactory[C]): Collector.Aux[(K, V), C[K, V]] =
3945
make(Builder.fromMapFactory(f))
4046

47+
implicit def supportsSortedMapFactory[K: Ordering, V, C[_, _]](
48+
f: SortedMapFactory[C]
49+
): Collector.Aux[(K, V), C[K, V]] =
50+
make(Builder.fromSortedMapFactory(f))
51+
52+
implicit def supportsEvidenceIterableFactory[A, C[_], E[_]](f: EvidenceIterableFactory[C, E])(
53+
implicit ev: E[A]
54+
): Collector.Aux[A, C[A]] =
55+
make(Builder.fromEvidenceIterableFactory(f))
56+
4157
/** Use `ArraySeq.untagged` to build a `Collector` where a `ClassTag` is not available.
4258
*/
4359
implicit def supportsTaggedArraySeq[A: ClassTag](
@@ -64,6 +80,16 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
6480
def fromMapFactory[K, V, C[_, _]](f: MapFactory[C]): Builder[(K, V), C[K, V]] =
6581
fromBuilder(f.newBuilder)
6682

83+
def fromSortedMapFactory[K: Ordering, V, C[_, _]](
84+
f: SortedMapFactory[C]
85+
): Builder[(K, V), C[K, V]] =
86+
fromBuilder(f.newBuilder)
87+
88+
def fromEvidenceIterableFactory[A, C[_], E[_]](f: EvidenceIterableFactory[C, E])(implicit
89+
ev: E[A]
90+
): Builder[A, C[A]] =
91+
fromBuilder(f.newBuilder)
92+
6793
def taggedArraySeq[A: ClassTag]: Builder[A, ArraySeq[A]] =
6894
array[A].mapResult(ArraySeq.unsafeWrapArray)
6995
}

core/shared/src/main/scala-3/fs2/CollectorPlatform.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
package fs2
2323

2424
import scala.collection.immutable.ArraySeq
25-
import scala.collection.{Factory, IterableFactory, MapFactory}
25+
import scala.collection.{
26+
Factory,
27+
EvidenceIterableFactory,
28+
IterableFactory,
29+
MapFactory,
30+
SortedMapFactory
31+
}
2632
import scala.reflect.ClassTag
2733

2834
private[fs2] trait CollectorPlatform { self: Collector.type =>
@@ -38,6 +44,16 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
3844
implicit def supportsMapFactory[K, V, C[_, _]](f: MapFactory[C]): Collector.Aux[(K, V), C[K, V]] =
3945
make(Builder.fromMapFactory(f))
4046

47+
implicit def supportsSortedMapFactory[K: Ordering, V, C[_, _]](
48+
f: SortedMapFactory[C]
49+
): Collector.Aux[(K, V), C[K, V]] =
50+
make(Builder.fromSortedMapFactory(f))
51+
52+
implicit def supportsEvidenceIterableFactory[A, C[_], E[_]](f: EvidenceIterableFactory[C, E])(
53+
implicit ev: E[A]
54+
): Collector.Aux[A, C[A]] =
55+
make(Builder.fromEvidenceIterableFactory(f))
56+
4157
/** Use `ArraySeq.untagged` to build a `Collector` where a `ClassTag` is not available.
4258
*/
4359
implicit def supportsTaggedArraySeq[A: ClassTag](
@@ -72,6 +88,16 @@ private[fs2] trait CollectorPlatform { self: Collector.type =>
7288
def fromMapFactory[K, V, C[_, _]](f: MapFactory[C]): Builder[(K, V), C[K, V]] =
7389
fromBuilder(f.newBuilder)
7490

91+
def fromSortedMapFactory[K: Ordering, V, C[_, _]](
92+
f: SortedMapFactory[C]
93+
): Builder[(K, V), C[K, V]] =
94+
fromBuilder(f.newBuilder)
95+
96+
def fromEvidenceIterableFactory[A, C[_], E[_]](f: EvidenceIterableFactory[C, E])(implicit
97+
ev: E[A]
98+
): Builder[A, C[A]] =
99+
fromBuilder(f.newBuilder)
100+
75101
def taggedArraySeq[A: ClassTag]: Builder[A, ArraySeq[A]] =
76102
array[A].mapResult(ArraySeq.unsafeWrapArray)
77103
}

core/shared/src/test/scala/fs2/CompilationTest.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,8 @@ object ThisModuleShouldCompile {
120120
Stream(1, 2, 3).compile.to(Set)
121121
Stream(1, 2, 3).to(List)
122122
Stream(1, 2, 3).covary[Fallible].to(List)
123+
Stream(1, 2, 3).to(Set)
124+
Stream(1, 2, 3).to(collection.immutable.SortedSet)
125+
Stream(1 -> 1, 2 -> 2, 3 -> 3).to(Map)
126+
Stream(1 -> 1, 2 -> 2, 3 -> 3).to(collection.immutable.SortedMap)
123127
}

0 commit comments

Comments
 (0)