summaryrefslogtreecommitdiff
path: root/docs/examples/tcpoly/collection/HOSeq.scala
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/tcpoly/collection/HOSeq.scala')
-rw-r--r--docs/examples/tcpoly/collection/HOSeq.scala46
1 files changed, 23 insertions, 23 deletions
diff --git a/docs/examples/tcpoly/collection/HOSeq.scala b/docs/examples/tcpoly/collection/HOSeq.scala
index a9414e3b3c..a6757b95ba 100644
--- a/docs/examples/tcpoly/collection/HOSeq.scala
+++ b/docs/examples/tcpoly/collection/HOSeq.scala
@@ -6,40 +6,40 @@ trait HOSeq {
// values implementing this interface, in order to provide more performant ways of building that structure
trait Accumulator[+coll[x], elT] {
def += (el: elT): Unit
- def result: coll[elT]
+ def result: coll[elT]
}
-
-
+
+
// Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor)
- // m[x] is intentionally unbounded: fold can then be defined nicely
- // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type
+ // m[x] is intentionally unbounded: fold can then be defined nicely
+ // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type
// is an invariant position -- should probably rule that out?
trait Iterable[+m[+x], +t] {
//def unit[a](orig: a): m[a]
def iterator: Iterator[t]
-
+
// construct an empty accumulator that will produce the same structure as this iterable, with elements of type t
def accumulator[t]: Accumulator[m, t]
-
+
def filter(p: t => Boolean): m[t] = {
val buf = accumulator[t]
val elems = elements
while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x }
buf.result
}
-
+
def map[s](f: t => s): m[s] = {
val buf = accumulator[s]
val elems = elements
while (elems.hasNext) buf += f(elems.next)
buf.result
}
-
+
// flatMap is a more specialized map, it only works if the mapped function produces Iterable values,
// which are then added to the result one by one
// the compiler should be able to find the right accumulator (implicit buf) to build the result
// to get concat, resColl = SingletonIterable, f = unit for SingletonIterable
- def flatMap[resColl[x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = {
+ def flatMap[resColl[x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = {
// TODO: would a viewbound for resColl[x] be better?
// -- 2nd-order type params are not yet in scope in view bound
val elems = elements
@@ -48,9 +48,9 @@ trait HOSeq {
while (elemss.hasNext) buf += elemss.next
}
buf.result
- }
+ }
}
-
+
final class ListBuffer[A] {
private var start: List[A] = Nil
private var last: ::[A] = _
@@ -78,7 +78,7 @@ trait HOSeq {
exported = !start.isEmpty
start
}
-
+
/** Clears the buffer contents.
*/
def clear: unit = {
@@ -97,13 +97,13 @@ trait HOSeq {
}
}
}
-
+
implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] {
private[this] val buff = new ListBuffer[elT]
def += (el: elT): Unit = buff += el
def result: List[elT] = buff.toList
}
-
+
trait List[+t] extends Iterable[List, t] {
def head: t
def tail: List[t]
@@ -111,16 +111,16 @@ trait HOSeq {
def iterator: Iterator[t] = error("TODO")
// construct an empty accumulator that will produce the same structure as this iterable, with elements of type t
- def accumulator[t]: Accumulator[List, t] = error("TODO")
+ def accumulator[t]: Accumulator[List, t] = error("TODO")
}
-
+
// TODO: the var tl approach does not seem to work because subtyping isn't fully working yet
final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] {
def head = hd
def tail = if(tl==null) this else tl // hack
override def isEmpty: boolean = false
}
-
+
case object Nil extends List[Nothing] {
def isEmpty = true
def head: Nothing =
@@ -149,18 +149,18 @@ trait HOSeq {
def filter(f: T=>Boolean): FilterResult
def subseq(from: int, to: int): Subseq
def flatMap[S <: Seq[K], K](f: T => S): S#Concat // legal?
- def concat(others: Seq[T]): Concat
+ def concat(others: Seq[T]): Concat
*/
-
+
/*trait Iterator[t] {
// @post hasAdvanced implies hasNext
// model def hasAdvanced: Boolean
-
+
def hasNext: Boolean // pure
-
+
// @pre hasAdvanced
def current: t // pure
-
+
// @pre hasNext
// @post hasAdvanced
def advance: Unit