Skip to content

Commit b396f5f

Browse files
authored
Merge pull request #18 from odersky/add-basetraits
Add base traits of collection hierarchy
2 parents 4912455 + e3fc2a2 commit b396f5f

File tree

20 files changed

+577
-93
lines changed

20 files changed

+577
-93
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ target/
1010
.history
1111
.DS_Store
1212
/tmp/
13+
/bin/

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package strawman.collection
1+
package strawman
2+
package collection
23

34
import scala.annotation.unchecked.uncheckedVariance
45
import scala.reflect.ClassTag
56
import scala.{Int, Boolean, Array, Any, Unit, StringContext}
6-
import java.lang.String
7-
7+
import java.lang.{String, UnsupportedOperationException}
88
import strawman.collection.mutable.{ArrayBuffer, StringBuilder}
99

1010
/** Base trait for generic collections */
@@ -54,8 +54,10 @@ trait IterableOps[+A] extends Any {
5454
protected def coll: Iterable[A]
5555
private def iterator() = coll.iterator()
5656

57-
/** Apply `f` to each element for tis side effects */
58-
def foreach(f: A => Unit): Unit = iterator().foreach(f)
57+
/** Apply `f` to each element for its side effects
58+
* Note: [U] parameter needed to help scalac's type inference.
59+
*/
60+
def foreach[U](f: A => U): Unit = iterator().foreach(f)
5961

6062
/** Fold left */
6163
def foldLeft[B](z: B)(op: (B, A) => B): B = iterator().foldLeft(z)(op)
@@ -166,7 +168,10 @@ trait IterableMonoTransforms[+A, +Repr] extends Any {
166168
def drop(n: Int): Repr = fromIterableWithSameElemType(View.Drop(coll, n))
167169

168170
/** The rest of the collection without its first element. */
169-
def tail: Repr = drop(1)
171+
def tail: Repr = {
172+
if (coll.isEmpty) throw new UnsupportedOperationException
173+
drop(1)
174+
}
170175
}
171176

172177
/** Transforms over iterables that can return collections of different element types.

src/main/scala/strawman/collection/IterableOnce.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package strawman.collection
2-
3-
import strawman.collection.mutable.Iterator
1+
package strawman
2+
package collection
43

54
trait IterableOnce[+A] {
65
/** Iterator can be used only once */

src/main/scala/strawman/collection/mutable/Iterator.scala renamed to src/main/scala/strawman/collection/Iterator.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package strawman.collection.mutable
1+
package strawman.collection
22

33
import scala.{Boolean, Int, Unit, Nothing, NoSuchElementException}
4-
import strawman.collection.{IndexedView, IterableOnce}
54

65
/** A core Iterator class */
7-
trait Iterator[+A] { self =>
6+
trait Iterator[+A] extends IterableOnce[A] { self =>
87
def hasNext: Boolean
98
def next(): A
9+
def iterator() = this
1010
def foldLeft[B](z: B)(op: (B, A) => B): B =
1111
if (hasNext) foldLeft(op(z, next()))(op) else z
1212
def foldRight[B](z: B)(op: (A, B) => B): B =
1313
if (hasNext) op(next(), foldRight(z)(op)) else z
14-
def foreach(f: A => Unit): Unit =
14+
def foreach[U](f: A => U): Unit =
1515
while (hasNext) f(next())
1616
def indexWhere(p: A => Boolean): Int = {
1717
var i = 0

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package strawman.collection
22

33
import scala.{Any, Boolean, Int, IndexOutOfBoundsException}
4-
import strawman.collection.mutable.Iterator
54
import strawman.collection.immutable.{List, Nil}
65

76
import scala.annotation.unchecked.uncheckedVariance
@@ -48,7 +47,6 @@ trait IndexedSeq[+A] extends Seq[A] { self =>
4847
}
4948
}
5049

51-
5250
/** Base trait for Seq operations */
5351
trait SeqLike[+A, +C[X] <: Seq[X]]
5452
extends IterableLike[A, C]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package strawman
2+
package collection
3+
4+
trait Set[A] extends Iterable[A] {}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package strawman.collection
22

33
import scala.{Int, Boolean, Nothing, annotation}
44
import scala.Predef.intWrapper
5-
import strawman.collection.mutable.Iterator
65

76
/** Concrete collection type: View */
87
trait View[+A] extends Iterable[A] with IterableLike[A, View] {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package strawman.collection.immutable
2+
import strawman.collection
3+
4+
trait Iterable[+A] extends collection.Iterable[A]
5+
with collection.IterableLike[A, Iterable]

src/main/scala/strawman/collection/immutable/LazyList.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package strawman.collection.immutable
22

33
import scala.{Option, Some, None, Nothing, StringContext}
4-
import strawman.collection.{IterableFactory, Iterable, LinearSeq, SeqLike}
5-
import strawman.collection.mutable.Iterator
4+
import strawman.collection
5+
import strawman.collection.{IterableFactory, LinearSeq, SeqLike, Iterator}
66

77
class LazyList[+A](expr: => LazyList.Evaluated[A])
8-
extends LinearSeq[A] with SeqLike[A, LazyList] {
8+
extends Seq[A] with SeqLike[A, LazyList] with LinearSeq[A] {
99
private[this] var evaluated = false
1010
private[this] var result: LazyList.Evaluated[A] = _
1111

@@ -23,7 +23,7 @@ class LazyList[+A](expr: => LazyList.Evaluated[A])
2323

2424
def #:: [B >: A](elem: => B): LazyList[B] = new LazyList(Some((elem, this)))
2525

26-
def fromIterable[B](c: Iterable[B]): LazyList[B] = LazyList.fromIterable(c)
26+
def fromIterable[B](c: collection.Iterable[B]): LazyList[B] = LazyList.fromIterable(c)
2727

2828
override def className = "LazyList"
2929

@@ -46,7 +46,7 @@ object LazyList extends IterableFactory[LazyList] {
4646
def unapply[A](s: LazyList[A]): Evaluated[A] = s.force
4747
}
4848

49-
def fromIterable[B](coll: Iterable[B]): LazyList[B] = coll match {
49+
def fromIterable[B](coll: collection.Iterable[B]): LazyList[B] = coll match {
5050
case coll: LazyList[B] => coll
5151
case _ => fromIterator(coll.iterator())
5252
}

src/main/scala/strawman/collection/immutable/List.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ package strawman.collection.immutable
33
import scala.annotation.unchecked.uncheckedVariance
44
import scala.Nothing
55
import scala.Predef.???
6-
import strawman.collection.{Iterable, IterableFactory, IterableOnce, LinearSeq, SeqLike}
6+
import strawman.collection
7+
import strawman.collection.{IterableFactory, IterableOnce, LinearSeq, SeqLike}
78
import strawman.collection.mutable.{Buildable, ListBuffer}
89

910

1011
/** Concrete collection type: List */
1112
sealed trait List[+A]
12-
extends LinearSeq[A]
13-
with SeqLike[A, List]
14-
with Buildable[A, List[A]] {
13+
extends Seq[A]
14+
with SeqLike[A, List]
15+
with LinearSeq[A]
16+
with Buildable[A, List[A]] {
1517

16-
def fromIterable[B](c: Iterable[B]): List[B] = List.fromIterable(c)
18+
def fromIterable[B](c: collection.Iterable[B]): List[B] = List.fromIterable(c)
1719

1820
protected[this] def newBuilder = new ListBuffer[A].mapResult(_.toList)
1921

@@ -48,8 +50,8 @@ case object Nil extends List[Nothing] {
4850
}
4951

5052
object List extends IterableFactory[List] {
51-
def fromIterable[B](coll: Iterable[B]): List[B] = coll match {
53+
def fromIterable[B](coll: collection.Iterable[B]): List[B] = coll match {
5254
case coll: List[B] => coll
5355
case _ => ListBuffer.fromIterable(coll).toList
5456
}
55-
}
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package strawman.collection.immutable
2+
3+
import strawman.collection
4+
5+
trait Seq[+A] extends collection.Seq[A]
6+
with collection.SeqLike[A, Seq]
7+
with Iterable[A]
8+
9+

0 commit comments

Comments
 (0)