1
1
package strawman
2
2
package collection
3
3
4
- import strawman .collection .mutable .Builder
4
+ import scala .language .implicitConversions
5
+
6
+ import strawman .collection .mutable .{ArrayBuffer , Builder }
5
7
6
8
import scala .{Any , Int , Nothing , Ordering }
7
9
import scala .annotation .unchecked .uncheckedVariance
8
10
11
+
12
+ /** Builds a collection of type `C` from elements of type `A` when a source collection of type `From` is available.
13
+ * Implicit instances of `BuildFrom` are available for all collection types.
14
+ *
15
+ * @tparam From Type of source collection
16
+ * @tparam A Type of elements (e.g. `Int`, `Boolean`, etc.)
17
+ * @tparam C Type of collection (e.g. `List[Int]`, `TreeMap[Int, String]`, etc.)
18
+ */
19
+ trait BuildFrom [- From , - A , + C ] extends Any {
20
+ def fromSpecificIterable (from : From )(it : Iterable [A ]): C
21
+ }
22
+
23
+ object BuildFrom {
24
+ /** Build the source collection type from an IterableOps */
25
+ implicit def buildFromIterableOps [C [X ] <: IterableOps [X , C , _], A , E ]: BuildFrom [C [A ], E , C [E ]] = new BuildFrom [C [A ], E , C [E ]] {
26
+ // TODO: Reuse a prototype instance
27
+ def fromSpecificIterable (from : C [A ])(it : Iterable [E ]): C [E ] = from.iterableFactory.fromIterable(it)
28
+ }
29
+
30
+ /** Build the source collection type from an Iterable with SortedOps */
31
+ implicit def buildFromSortedOps [C [X ] <: Iterable [X ] with SortedOps [X , C [X ], C ], A , E : Ordering ]: BuildFrom [C [A ], E , C [E ]] = new BuildFrom [C [A ], E , C [E ]] {
32
+ def fromSpecificIterable (from : C [A ])(it : Iterable [E ]): C [E ] = from.sortedIterableFactory.fromSpecificIterable(it)
33
+ }
34
+ }
35
+
36
+ /** A more specific `BuildFrom` for strict target collection types which can provide a `Builder`.
37
+ * Note that a `Builder` can be obtained for any `BuildFrom` via `Builder.from`.
38
+ */
39
+ trait StrictBuildFrom [- From , - A , + C ] extends Any with BuildFrom [From , A , C ] {
40
+ def newBuilder (from : From ): Builder [A , C ]
41
+ }
42
+
43
+ object StrictBuildFrom {
44
+ /** Build the source collection type from a strict IterableOps */
45
+ implicit def strictBuildFromIterableOps [C [X ] <: IterableOps [X , C , _] with Buildable [X , C [X ]], A , E ]: StrictBuildFrom [C [A ], E , C [E ]] = new StrictBuildFrom [C [A ], E , C [E ]] {
46
+ // TODO: Reuse a prototype instance
47
+ def newBuilder (from : C [A ]): Builder [E , C [E ]] = from.iterableFactory.asInstanceOf [IterableFactoryWithBuilder [C ]].newBuilder[E ]()
48
+ def fromSpecificIterable (from : C [A ])(it : Iterable [E ]): C [E ] = from.iterableFactory.fromIterable(it)
49
+ }
50
+
51
+ /** Build the source collection type from a strict Iterable with SortedOps */
52
+ implicit def strictBuildFromSortedOps [C [X ] <: Iterable [X ] with SortedOps [X , C [X ], C ] with Buildable [X , C [X ]], A , E : Ordering ]: StrictBuildFrom [C [A ], E , C [E ]] = new StrictBuildFrom [C [A ], E , C [E ]] {
53
+ def newBuilder (from : C [A ]): Builder [E , C [E ]] = from.sortedIterableFactory.asInstanceOf [SortedIterableFactoryWithBuilder [C ]].newBuilder[E ]()
54
+ def fromSpecificIterable (from : C [A ])(it : Iterable [E ]): C [E ] = from.sortedIterableFactory.fromSpecificIterable(it)
55
+ }
56
+ }
57
+
9
58
/**
10
59
* Builds a collection of type `C` from elements of type `A`
11
60
* @tparam A Type of elements (e.g. `Int`, `Boolean`, etc.)
12
61
* @tparam C Type of collection (e.g. `List[Int]`, `TreeMap[Int, String]`, etc.)
13
62
*/
14
- trait FromSpecificIterable [- A , + C ] extends Any {
63
+ trait FromSpecificIterable [- A , + C ] extends Any with BuildFrom [Any , A , C ] {
64
+ def fromSpecificIterable (from : Any )(it : Iterable [A ]): C = fromSpecificIterable(it)
15
65
def fromSpecificIterable (it : Iterable [A ]): C
16
66
}
17
67
68
+ /** A more specific `FromSpecificIterable` for strict collection types which can provide a `Builder`. */
69
+ trait FromSpecificIterableWithBuilder [- A , + C ] extends Any with FromSpecificIterable [A , C ] with StrictBuildFrom [Any , A , C ] {
70
+ def newBuilder (from : Any ): Builder [A , C ] = newBuilder
71
+ def newBuilder : Builder [A , C ]
72
+ }
73
+
18
74
/** Base trait for companion objects of unconstrained collection types */
19
75
trait IterableFactory [+ CC [_]] {
20
76
@@ -28,13 +84,7 @@ trait IterableFactory[+CC[_]] {
28
84
29
85
}
30
86
31
- trait IterableFactoryWithBuilder [+ CC [_]] extends IterableFactory [CC ] {
32
- def newBuilder [A ](): Builder [A , CC [A ]]
33
- }
34
-
35
87
object IterableFactory {
36
- import scala .language .implicitConversions
37
-
38
88
implicit def toSpecific [A , CC [_]](factory : IterableFactory [CC ]): FromSpecificIterable [A , CC [A ]] =
39
89
new FromSpecificIterable [A , CC [A ]] {
40
90
def fromSpecificIterable (it : Iterable [A ]): CC [A ] = factory.fromIterable[A ](it)
@@ -44,7 +94,24 @@ object IterableFactory {
44
94
def empty [A ]: CC [A ] = delegate.empty
45
95
def fromIterable [E ](it : Iterable [E ]): CC [E ] = delegate.fromIterable(it)
46
96
}
97
+ }
47
98
99
+ trait IterableFactoryWithBuilder [+ CC [_]] extends IterableFactory [CC ] {
100
+ def newBuilder [A ](): Builder [A , CC [A ]]
101
+ }
102
+
103
+ object IterableFactoryWithBuilder {
104
+ implicit def toSpecific [A , CC [_]](factory : IterableFactoryWithBuilder [CC ]): FromSpecificIterableWithBuilder [A , CC [A ]] =
105
+ new FromSpecificIterableWithBuilder [A , CC [A ]] {
106
+ def fromSpecificIterable (it : Iterable [A ]): CC [A ] = factory.fromIterable[A ](it)
107
+ def newBuilder : Builder [A , CC [A ]] = factory.newBuilder[A ]()
108
+ }
109
+
110
+ class Delegate [CC [_]](delegate : IterableFactoryWithBuilder [CC ]) extends IterableFactoryWithBuilder [CC ] {
111
+ def empty [A ]: CC [A ] = delegate.empty
112
+ def fromIterable [E ](it : Iterable [E ]): CC [E ] = delegate.fromIterable(it)
113
+ def newBuilder [A ](): Builder [A , CC [A ]] = delegate.newBuilder[A ]()
114
+ }
48
115
}
49
116
50
117
trait SpecificIterableFactory [- A , + C ] extends FromSpecificIterable [A , C ] {
@@ -55,6 +122,8 @@ trait SpecificIterableFactory[-A, +C] extends FromSpecificIterable[A, C] {
55
122
def fill (n : Int )(elem : => A ): C = fromSpecificIterable(View .Fill (n)(elem))
56
123
}
57
124
125
+ trait SpecificIterableFactoryWithBuilder [- A , + C ] extends SpecificIterableFactory [A , C ] with FromSpecificIterableWithBuilder [A , C ]
126
+
58
127
/** Factory methods for collections of kind `* −> * -> *` */
59
128
trait MapFactory [+ CC [X , Y ]] {
60
129
@@ -65,8 +134,6 @@ trait MapFactory[+CC[X, Y]] {
65
134
}
66
135
67
136
object MapFactory {
68
- import scala .language .implicitConversions
69
-
70
137
implicit def toSpecific [K , V , CC [X , Y ]](factory : MapFactory [CC ]): FromSpecificIterable [(K , V ), CC [K , V ]] =
71
138
new FromSpecificIterable [(K , V ), CC [K , V ]] {
72
139
def fromSpecificIterable (it : Iterable [(K , V )]): CC [K , V ] = factory.fromIterable[K , V ](it)
@@ -76,7 +143,6 @@ object MapFactory {
76
143
def fromIterable [K , V ](it : Iterable [(K , V )]): C [K , V ] = delegate.fromIterable(it)
77
144
def empty [K , V ]: C [K , V ] = delegate.empty
78
145
}
79
-
80
146
}
81
147
82
148
/** Base trait for companion objects of collections that require an implicit evidence */
@@ -92,8 +158,6 @@ trait SortedIterableFactory[+CC[_]] {
92
158
}
93
159
94
160
object SortedIterableFactory {
95
- import scala .language .implicitConversions
96
-
97
161
implicit def toSpecific [A : Ordering , CC [_]](factory : SortedIterableFactory [CC ]): FromSpecificIterable [A , CC [A ]] =
98
162
new FromSpecificIterable [A , CC [A ]] {
99
163
def fromSpecificIterable (it : Iterable [A ]): CC [A ] = factory.sortedFromIterable[A ](it)
@@ -103,7 +167,24 @@ object SortedIterableFactory {
103
167
def empty [A : Ordering ]: CC [A ] = delegate.empty
104
168
def sortedFromIterable [E : Ordering ](it : Iterable [E ]): CC [E ] = delegate.sortedFromIterable(it)
105
169
}
170
+ }
106
171
172
+ trait SortedIterableFactoryWithBuilder [+ CC [_]] extends SortedIterableFactory [CC ] {
173
+ def newBuilder [A : Ordering ](): Builder [A , CC [A ]]
174
+ }
175
+
176
+ object SortedIterableFactoryWithBuilder {
177
+ implicit def toSpecific [A : Ordering , CC [_]](factory : SortedIterableFactoryWithBuilder [CC ]): FromSpecificIterableWithBuilder [A , CC [A ]] =
178
+ new FromSpecificIterableWithBuilder [A , CC [A ]] {
179
+ def fromSpecificIterable (it : Iterable [A ]): CC [A ] = factory.sortedFromIterable[A ](it)
180
+ def newBuilder : Builder [A , CC [A ]] = factory.newBuilder[A ]()
181
+ }
182
+
183
+ class Delegate [CC [_]](delegate : SortedIterableFactoryWithBuilder [CC ]) extends SortedIterableFactoryWithBuilder [CC ] {
184
+ def empty [A : Ordering ]: CC [A ] = delegate.empty
185
+ def sortedFromIterable [E : Ordering ](it : Iterable [E ]): CC [E ] = delegate.sortedFromIterable(it)
186
+ def newBuilder [A : Ordering ](): Builder [A , CC [A ]] = delegate.newBuilder[A ]()
187
+ }
107
188
}
108
189
109
190
/** Factory methods for collections of kind `* −> * -> *` which require an implicit evidence value for the key type */
@@ -118,8 +199,6 @@ trait SortedMapFactory[+CC[X, Y]] {
118
199
}
119
200
120
201
object SortedMapFactory {
121
- import scala .language .implicitConversions
122
-
123
202
implicit def toSpecific [K : Ordering , V , CC [_, _]](factory : SortedMapFactory [CC ]): FromSpecificIterable [(K , V ), CC [K , V ]] =
124
203
new FromSpecificIterable [(K , V ), CC [K , V ]] {
125
204
def fromSpecificIterable (it : Iterable [(K , V )]): CC [K , V ] = factory.sortedFromIterable(it)
@@ -129,5 +208,4 @@ object SortedMapFactory {
129
208
def empty [K : Ordering , V ]: CC [K , V ] = delegate.empty[K , V ]
130
209
def sortedFromIterable [K : Ordering , V ](it : Iterable [(K , V )]): CC [K , V ] = delegate.sortedFromIterable(it)
131
210
}
132
-
133
211
}
0 commit comments