Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit 5f4a8cd

Browse files
committed
Maps
1 parent 960e273 commit 5f4a8cd

File tree

7 files changed

+126
-3
lines changed

7 files changed

+126
-3
lines changed

src/main/scala/strawman/collection/Iterable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait IterableLike[+A, +C[X] <: Iterable[X]]
3333
/** Create a collection of type `C[A]` from the elements of `coll`, which has
3434
* the same element type as this collection. Overridden in StringOps and ArrayOps.
3535
*/
36-
protected[this] def fromIterableWithSameElemType(coll: Iterable[A]): C[A] = fromIterable(coll)
36+
protected[this] def fromIterableWithSameElemType(coll: Iterable[A]): C[A]
3737
}
3838

3939
/** Base trait for instances that can construct a collection from an iterable */
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package strawman.collection
2+
3+
import strawman.collection.mutable.Builder
4+
5+
import scala.Option
6+
import scala.annotation.unchecked.uncheckedVariance
7+
import scala.Predef.???
8+
9+
/** Base Map type */
10+
trait Map[K, +V]
11+
extends Iterable[(K, V)]
12+
with MapLike[K, V, Map]
13+
14+
/** Base Map implementation type */
15+
trait MapLike[K, +V, +C[X, Y] <: Map[X, Y]]
16+
extends IterableLike[(K, V), Iterable]
17+
with IterableMonoTransforms[(K, V), C[K, V @uncheckedVariance]]
18+
with MapPolyTransforms[K, V, C] {
19+
20+
def get(key: K): Option[V]
21+
22+
}
23+
24+
/** Polymorphic transformation methods */
25+
trait MapPolyTransforms[K, +V, +C[X, Y] <: Map[X, Y]] extends IterablePolyTransforms[(K, V), Iterable] {
26+
27+
def map[K2, V2](f: (K, V) => (K2, V2)): C[K2, V2]
28+
29+
def flatMap[K2, V2](f: (K, V) => IterableOnce[(K2, V2)]): C[K2, V2]
30+
31+
}
32+
33+
/** Factory methods for collections of kind `* −> * -> *` */
34+
trait MapFactories[C[_, _]] {
35+
36+
def newBuilder[K, V]: Builder[(K, V), C[K, V]]
37+
38+
def empty[K, V]: C[K, V] =
39+
newBuilder[K, V].result
40+
41+
def apply[K, V](elems: (K, V)*): C[K, V] =
42+
newBuilder[K, V].++=(elems.toStrawman).result
43+
44+
}

src/main/scala/strawman/collection/Seq.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ trait IndexedSeq[+A] extends Seq[A] { self =>
5050
/** Base trait for Seq operations */
5151
trait SeqLike[+A, +C[X] <: Seq[X]]
5252
extends IterableLike[A, C]
53-
with SeqMonoTransforms[A, C[A @uncheckedVariance]] // sound bcs of VarianceNote
53+
with SeqMonoTransforms[A, C[A @uncheckedVariance]] { // sound bcs of VarianceNote
5454

55+
protected[this] def fromIterableWithSameElemType(coll: Iterable[A]): C[A] = fromIterable(coll)
56+
57+
}
5558
/** Base trait for linear Seq operations */
5659
trait LinearSeqLike[+A, +C[X] <: LinearSeq[X]] extends SeqLike[A, C] {
5760

src/main/scala/strawman/collection/View.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ trait View[+A] extends Iterable[A] with IterableLike[A, View] {
1212
case c: View[B] => c
1313
case _ => View.fromIterator(c.iterator())
1414
}
15+
16+
protected[this] def fromIterableWithSameElemType(coll: Iterable[A]): View[A] = fromIterable(coll)
17+
1518
override def className = "View"
1619
}
1720

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package strawman.collection.mutable
2+
3+
import strawman.collection.{IterableOnce, Iterator, MapFactories}
4+
5+
import scala.{Option, Unit}
6+
import scala.Predef.???
7+
8+
/** A mutable map backed by a hashtable */
9+
final class HashMap[K, V]
10+
extends Map[K, V]
11+
with MapLike[K, V, HashMap] {
12+
13+
// From IterableOnce
14+
def iterator(): Iterator[(K, V)] = ???
15+
16+
// From MapLike
17+
def get(key: K): Option[V] = ???
18+
19+
// From Growable
20+
def +=(elem: (K, V)): this.type = ???
21+
def clear(): Unit = ???
22+
23+
// From mutable.MapLike
24+
def -=(elem: (K, V)): this.type = ???
25+
def put(key: K, value: V): Option[V] = ???
26+
27+
// From MapPolyTransforms
28+
def map[K2, V2](f: (K, V) => (K2, V2)): HashMap[K2, V2] = ???
29+
def flatMap[K2, V2](f: (K, V) => IterableOnce[(K2, V2)]): HashMap[K2, V2] = ???
30+
31+
// From IterablePolyTransforms
32+
def fromIterable[B](coll: strawman.collection.Iterable[B]): Iterable[B] = ???
33+
// From IterableMonoTransforms
34+
protected[this] def fromIterableWithSameElemType(coll: strawman.collection.Iterable[(K, V)]): HashMap[K, V] = ???
35+
36+
}
37+
38+
object HashMap extends MapFactories[HashMap] {
39+
40+
def newBuilder[K, V]: Builder[(K, V), HashMap[K, V]] = ???
41+
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package strawman.collection.mutable
2+
3+
import strawman.collection.IterableMonoTransforms
4+
5+
import scala.Option
6+
7+
/** Base type of mutable Maps */
8+
trait Map[K, V]
9+
extends strawman.collection.Map[K, V]
10+
with MapLike[K, V, Map]
11+
12+
/** Base trait of mutable Maps implementations */
13+
trait MapLike[K, V, +C[X, Y] <: Map[X, Y]]
14+
extends strawman.collection.MapLike[K, V, C]
15+
with Iterable[(K, V)]
16+
with Growable[(K, V)] {
17+
18+
def -= (elem: (K, V)): this.type
19+
20+
def put(key: K, value: V): Option[V]
21+
22+
}

src/test/scala/strawman/collection/test/Test.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class StrawmanTest {
130130
val ys9: Seq[Int] = xs9
131131
val xs9a = xs.map(_.toUpper)
132132
val ys9a: String = xs9a
133-
val xs10 = xs.flatMap((x: Char) => s"$x,$x")
133+
val xs10 = xs.flatMap(x => s"$x,$x")
134134
val ys10: String = xs10
135135
val xs11 = xs ++ xs
136136
val ys11: String = xs11
@@ -288,6 +288,15 @@ class StrawmanTest {
288288
println(xs17.to(List))
289289
}
290290

291+
def mapOps(xs: strawman.collection.Map[Int, String]): Unit = {
292+
// val xs1 = xs.map { case (k, v) => (v, k) }
293+
// val xs2: strawman.collection.Map[String, Int] = xs1
294+
val xs1 = xs.map(kv => (kv._2, kv._1))
295+
val xs2: strawman.collection.Iterable[(String, Int)] = xs1
296+
println(xs1)
297+
println(xs2)
298+
}
299+
291300
@Test
292301
def mainTest: Unit = {
293302
val ints = List(1, 2, 3)

0 commit comments

Comments
 (0)