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

Commit

Permalink
Merge pull request #18 from odersky/add-basetraits
Browse files Browse the repository at this point in the history
Add base traits of collection hierarchy
  • Loading branch information
odersky authored Jan 22, 2017
2 parents 4912455 + e3fc2a2 commit b396f5f
Show file tree
Hide file tree
Showing 20 changed files with 577 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ target/
.history
.DS_Store
/tmp/
/bin/
17 changes: 11 additions & 6 deletions src/main/scala/strawman/collection/Iterable.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package strawman.collection
package strawman
package collection

import scala.annotation.unchecked.uncheckedVariance
import scala.reflect.ClassTag
import scala.{Int, Boolean, Array, Any, Unit, StringContext}
import java.lang.String

import java.lang.{String, UnsupportedOperationException}
import strawman.collection.mutable.{ArrayBuffer, StringBuilder}

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

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

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

/** The rest of the collection without its first element. */
def tail: Repr = drop(1)
def tail: Repr = {
if (coll.isEmpty) throw new UnsupportedOperationException
drop(1)
}
}

/** Transforms over iterables that can return collections of different element types.
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/strawman/collection/IterableOnce.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package strawman.collection

import strawman.collection.mutable.Iterator
package strawman
package collection

trait IterableOnce[+A] {
/** Iterator can be used only once */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package strawman.collection.mutable
package strawman.collection

import scala.{Boolean, Int, Unit, Nothing, NoSuchElementException}
import strawman.collection.{IndexedView, IterableOnce}

/** A core Iterator class */
trait Iterator[+A] { self =>
trait Iterator[+A] extends IterableOnce[A] { self =>
def hasNext: Boolean
def next(): A
def iterator() = this
def foldLeft[B](z: B)(op: (B, A) => B): B =
if (hasNext) foldLeft(op(z, next()))(op) else z
def foldRight[B](z: B)(op: (A, B) => B): B =
if (hasNext) op(next(), foldRight(z)(op)) else z
def foreach(f: A => Unit): Unit =
def foreach[U](f: A => U): Unit =
while (hasNext) f(next())
def indexWhere(p: A => Boolean): Int = {
var i = 0
Expand Down
2 changes: 0 additions & 2 deletions src/main/scala/strawman/collection/Seq.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package strawman.collection

import scala.{Any, Boolean, Int, IndexOutOfBoundsException}
import strawman.collection.mutable.Iterator
import strawman.collection.immutable.{List, Nil}

import scala.annotation.unchecked.uncheckedVariance
Expand Down Expand Up @@ -48,7 +47,6 @@ trait IndexedSeq[+A] extends Seq[A] { self =>
}
}


/** Base trait for Seq operations */
trait SeqLike[+A, +C[X] <: Seq[X]]
extends IterableLike[A, C]
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/strawman/collection/Set.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package strawman
package collection

trait Set[A] extends Iterable[A] {}
1 change: 0 additions & 1 deletion src/main/scala/strawman/collection/View.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package strawman.collection

import scala.{Int, Boolean, Nothing, annotation}
import scala.Predef.intWrapper
import strawman.collection.mutable.Iterator

/** Concrete collection type: View */
trait View[+A] extends Iterable[A] with IterableLike[A, View] {
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/strawman/collection/immutable/Iterable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package strawman.collection.immutable
import strawman.collection

trait Iterable[+A] extends collection.Iterable[A]
with collection.IterableLike[A, Iterable]
10 changes: 5 additions & 5 deletions src/main/scala/strawman/collection/immutable/LazyList.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package strawman.collection.immutable

import scala.{Option, Some, None, Nothing, StringContext}
import strawman.collection.{IterableFactory, Iterable, LinearSeq, SeqLike}
import strawman.collection.mutable.Iterator
import strawman.collection
import strawman.collection.{IterableFactory, LinearSeq, SeqLike, Iterator}

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

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

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

def fromIterable[B](c: Iterable[B]): LazyList[B] = LazyList.fromIterable(c)
def fromIterable[B](c: collection.Iterable[B]): LazyList[B] = LazyList.fromIterable(c)

override def className = "LazyList"

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

def fromIterable[B](coll: Iterable[B]): LazyList[B] = coll match {
def fromIterable[B](coll: collection.Iterable[B]): LazyList[B] = coll match {
case coll: LazyList[B] => coll
case _ => fromIterator(coll.iterator())
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/strawman/collection/immutable/List.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package strawman.collection.immutable
import scala.annotation.unchecked.uncheckedVariance
import scala.Nothing
import scala.Predef.???
import strawman.collection.{Iterable, IterableFactory, IterableOnce, LinearSeq, SeqLike}
import strawman.collection
import strawman.collection.{IterableFactory, IterableOnce, LinearSeq, SeqLike}
import strawman.collection.mutable.{Buildable, ListBuffer}


/** Concrete collection type: List */
sealed trait List[+A]
extends LinearSeq[A]
with SeqLike[A, List]
with Buildable[A, List[A]] {
extends Seq[A]
with SeqLike[A, List]
with LinearSeq[A]
with Buildable[A, List[A]] {

def fromIterable[B](c: Iterable[B]): List[B] = List.fromIterable(c)
def fromIterable[B](c: collection.Iterable[B]): List[B] = List.fromIterable(c)

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

Expand Down Expand Up @@ -48,8 +50,8 @@ case object Nil extends List[Nothing] {
}

object List extends IterableFactory[List] {
def fromIterable[B](coll: Iterable[B]): List[B] = coll match {
def fromIterable[B](coll: collection.Iterable[B]): List[B] = coll match {
case coll: List[B] => coll
case _ => ListBuffer.fromIterable(coll).toList
}
}
}
9 changes: 9 additions & 0 deletions src/main/scala/strawman/collection/immutable/Seq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package strawman.collection.immutable

import strawman.collection

trait Seq[+A] extends collection.Seq[A]
with collection.SeqLike[A, Seq]
with Iterable[A]


Loading

0 comments on commit b396f5f

Please sign in to comment.