Created
April 3, 2019 07:56
-
-
Save lrytz/82910a7754f5d33b1dc2693f9eb37071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import annotation.unchecked.uncheckedVariance | |
trait Bldr[-A, +To] { self => | |
def result(): To | |
} | |
trait ItFact[+CC[_]] { | |
def from[A](source: It[A]): CC[A] | |
def newBuilder[A]: Bldr[A, CC[A]] | |
} | |
trait ItFactDefaults[+A, +CC[_]] extends ItOps[A, CC, CC[A @uncheckedVariance]] { | |
protected def fromSpecific(coll: It[A @uncheckedVariance]): CC[A @uncheckedVariance] = iterableFactory.from(coll) | |
protected def newSpecificBuilder: Bldr[A @uncheckedVariance, CC[A @uncheckedVariance]] = iterableFactory.newBuilder[A] | |
} | |
trait ItOps[+A, +CC[_], +C] { | |
def it: It[A] | |
protected def newSpecificBuilder: Bldr[A @uncheckedVariance, C] | |
protected def fromSpecific(coll: It[A @uncheckedVariance]): C | |
def iterableFactory: ItFact[CC] | |
def filter: C = fromSpecific(it) | |
def strictFilter: C = newSpecificBuilder.result() | |
def map[B](f: A => B): CC[B] = iterableFactory.newBuilder.result() | |
} | |
trait It[+A] extends ItOps[A, It, It[A]] with ItFactDefaults[A, It] { | |
def it: It[A] = this | |
def iterableFactory: ItFact[It] = It | |
} | |
object It extends ItFact[It] { | |
def from[A](source: It[A]): It[A] = new It[A]{} | |
def newBuilder[A]: Bldr[A,It[A]] = new Bldr[A, It[A]] { def result(): It[A] = new It[A]{} } | |
} | |
trait BF[-From, -A, +C] { | |
def fromSpecific(from: From)(it: It[A]): C | |
def newBuilder(from: From): Bldr[A, C] | |
} | |
object BF { | |
implicit def bfItOps[CC[X] <: It[X] with ItOps[X, CC, _], A0, A]: BF[CC[A0], A, CC[A]] = new BF[CC[A0], A, CC[A]] { | |
// def newBuilder(from: CC[A0]): Bldr[A, CC[A]] = (from: ItOps[A0, CC, _]).iterableFactory.newBuilder[A] | |
def newBuilder(from: CC[A0]): Bldr[A, CC[A]] = from.iterableFactory.newBuilder[A] | |
def fromSpecific(from: CC[A0])(it: It[A]): CC[A] = (from: ItOps[A0, CC, _]).iterableFactory.from(it) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment