forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterableSelfRec.scala
52 lines (40 loc) · 1.82 KB
/
IterableSelfRec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package dotty.collection
package immutable
import annotation.unchecked.uncheckedVariance
trait Collection[T] { self =>
type This <: Collection { type This <: self.This }
def companion: CollectionCompanion[This]
}
trait Iterable[T] extends Collection[T] { self =>
type This <: Iterable { type This <: self.This }
override def companion: IterableCompanion[This] = Iterable.asInstanceOf
def iterator: Iterator[T]
}
trait Seq[T] extends Iterable[T] { self =>
type This <: Seq { type This <: self.This }
override def companion: IterableCompanion[This] = Seq.asInstanceOf
def apply(x: Int): T
}
abstract class CollectionCompanion[+CC <: Collection { type This <: CC }]
abstract class IterableCompanion[+CC <: Iterable { type This <: CC }] extends CollectionCompanion[CC] {
def fromIterator[T](it: Iterator[T]): CC[T]
def map[T, U](xs: Iterable[T], f: T => U): CC[U] =
fromIterator(xs.iterator.map(f))
def filter[T](xs: Iterable[T], p: T => Boolean): CC[T] =
fromIterator(xs.iterator.filter(p))
def flatMap[T, U](xs: Iterable[T], f: T => TraversableOnce[U]): CC[U] =
fromIterator(xs.iterator.flatMap(f))
implicit def transformOps[T](xs: CC[T] @uncheckedVariance): TransformOps[CC, T] = ??? // new TransformOps[CC, T](xs)
}
class TransformOps[+CC <: Iterable { type This <: CC }, T] (val xs: CC[T]) extends AnyVal {
def companion[T](xs: CC[T] @uncheckedVariance): IterableCompanion[CC] = xs.companion
def map[U](f: T => U): CC[U] = companion(xs).map(xs, f)
def filter(p: T => Boolean): CC[T] = companion(xs).filter(xs, p)
def flatMap[U](f: T => TraversableOnce[U]): CC[U] = companion(xs).flatMap(xs, f)
}
object Iterable extends IterableCompanion[Iterable] {
def fromIterator[T](it: Iterator[T]): Iterable[T] = ???
}
object Seq extends IterableCompanion[Seq] {
def fromIterator[T](it: Iterator[T]): Seq[T] = ???
}