Skip to content

Commit

Permalink
compiling with different type classes
Browse files Browse the repository at this point in the history
  • Loading branch information
wheaties committed Dec 28, 2013
1 parent ed9ac56 commit 5ed7c06
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
2 changes: 0 additions & 2 deletions choices/src/main/scala/com/wheaties/choice/Choice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ trait Choice[-Value]{

def set[C, A](collection: C, value: A)(implicit replace: Replace[Value, C, A]) = replace(collection, value, filter)

//def set[C, V <: Value](collection: C, value: V)(implicit modify: Modify[V, C]) = modify(collection, _ => value, filter)

def mod[C, V <: Value](collection: C, f: V => V)(implicit modify: Modify[V, C]) = modify(collection, f, filter)

def compose[V <: Value](that: Choice[V]) = that andThen this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ trait ModifyImplicits{
}
}

implicit def modArray[Elem, Repr <: ArrayLike[Elem, Repr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) =
implicit def modArray[Elem](implicit cbf: CanBuildFrom[Array[Elem], Elem, Array[Elem]]) = new Modify[Elem, Array[Elem]] {
def apply(coll: Array[Elem], f: Elem => Elem, pred: Elem => Boolean): Array[Elem] ={
def sub(elem: Elem) = if(pred(elem)) f(elem) else elem

coll.map(sub)(cbf)
}
}

implicit def modArrayLike[Elem, Repr <: ArrayLike[Elem, Repr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) =
new Modify[Elem, Repr] {
def apply(coll: Repr, f: Elem => Elem, pred: Elem => Boolean): Repr ={
def sub(elem: Elem) = if(pred(elem)) f(elem) else elem
Expand Down
91 changes: 64 additions & 27 deletions choices/src/main/scala/com/wheaties/choice/iteration/Replace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TAReplace[+Elem, Repr <: TraversableLike[Elem, Repr], V <: Elem](implicit
}
}

class TAlikeReplace[+Elem, Repr <: TraversableLike[Elem, Repr], V <: Elem, ARepr <: ArrayLike[V, ARepr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr])
class TAlikeReplace[Elem, Repr <: TraversableLike[Elem, Repr], ARepr <: ArrayLike[Elem, ARepr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr])
extends Replace[Elem, Repr, ARepr]{

def apply(repr: Repr, value: ARepr, pred: Elem => Boolean): Repr ={
Expand All @@ -51,9 +51,8 @@ class TAlikeReplace[+Elem, Repr <: TraversableLike[Elem, Repr], V <: Elem, ARepr
}
}


trait ReplaceImplicits{
implicit def abuffer[E, V <: E] = new TVReplace[E, ArrayBuffer[E], V]
implicit def abufferV[E, V <: E] = new TVReplace[E, ArrayBuffer[E], V]
implicit def hashsetV[E, V <: E] = new TVReplace[E, HashSet[E], V]
implicit def indexedV[E, V <: E] = new TVReplace[E, IndexedSeq[E], V]
implicit def iterV[E, V <: E] = new TVReplace[E, Iterable[E], V]
Expand All @@ -67,14 +66,14 @@ trait ReplaceImplicits{
implicit def mlistV[E, V <: E] = new TVReplace[E, MutableList[E], V]
implicit def seqV[E, V <: E] = new TVReplace[E, Seq[E], V]
implicit def setV[E, V <: E] = new TVReplace[E, Set[E], V]
implicit def sortedsetV[E, V <: E] = new TVReplace[E, SortedSet[E], V]
implicit def sortedsetV[E: Ordering, V <: E] = new TVReplace[E, SortedSet[E], V]
implicit def stackV[E, V <: E] = new TVReplace[E, Stack[E], V]
implicit def streamV[E, V <: E] = new TVReplace[E, Stream[E], V]
implicit def travV[E, V <: E] = new TVReplace[E, Traversable[E], V]
implicit def treesetV[E, V <: E] = new TVReplace[E, TreeSet[E], V]
implicit def treesetV[E: Ordering, V <: E] = new TVReplace[E, TreeSet[E], V]
implicit def vectorV[E, V <: E] = new TVReplace[E, Vector[E], V]

implicit def abuffer[E, Repr <: TraversableLike[E, Repr]] = new TVReplace[E, ArrayBuffer[E], Repr]
implicit def abufferT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, ArrayBuffer[E], Repr]
implicit def hashsetT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, HashSet[E], Repr]
implicit def indexedT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, IndexedSeq[E], Repr]
implicit def iterT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Iterable[E], Repr]
Expand All @@ -88,53 +87,91 @@ trait ReplaceImplicits{
implicit def mlistT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, MutableList[E], Repr]
implicit def seqT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Seq[E], Repr]
implicit def setT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Set[E], Repr]
implicit def sortedsetT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, SortedSet[E], Repr]
implicit def sortedsetT[E: Ordering, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, SortedSet[E], Repr]
implicit def stackT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Stack[E], Repr]
implicit def streamT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Stream[E], Repr]
implicit def travT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Traversable[E], Repr]
implicit def treesetT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, TreeSet[E], Repr]
implicit def treesetT[E: Ordering, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, TreeSet[E], Repr]
implicit def vectorT[E, Repr <: TraversableLike[E, Repr]] = new TTReplace[E, Vector[E], Repr]

implicit def abufferA[E, V <: E] = new TAReplace[E, ArrayBuffer[E], V]
implicit def hashsetA[E, V <: E] = new TAReplace[E, HashSet[E], V]
implicit def indexedA[E, V <: E] = new TAReplace[E, IndexedSeq[E], V]
implicit def iterA[E, V <: E] = new TAReplace[E, Iterable[E], V]
implicit def listA[E, V <: E] = new TAReplace[E, List[E], V]
implicit def setA[E, V <: E] = new TAReplace[E, Set[E], V]
implicit def linseqA[E, V <: E] = new TAReplace[E, LinearSeq[E], V]
implicit def linkhsA[E, V <: E] = new TAReplace[E, LinkedHashSet[E], V]
implicit def linklA[E, V <: E] = new TAReplace[E, LinkedList[E], V]
implicit def listbA[E, V <: E] = new TAReplace[E, ListBuffer[E], V]
implicit def listsetA[E, V <: E] = new TAReplace[E, ListSet[E], V]
implicit def msetA[E, V <: E] = new TAReplace[E, MSet[E], V]
implicit def mlistA[E, V <: E] = new TAReplace[E, MutableList[E], V]
implicit def seqA[E, V <: E] = new TAReplace[E, Seq[E], V]

implicit def repArray[Elem, Repr <: ArrayLike[Elem, Repr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) =
new Replace[Elem, Repr, Elem] {
def apply(coll: Repr, value: Elem, pred: Elem => Boolean): Repr ={
implicit def setA[E, V <: E] = new TAReplace[E, Set[E], V]
implicit def sortedsetA[E: Ordering, V <: E] = new TAReplace[E, SortedSet[E], V]
implicit def stackA[E, V <: E] = new TAReplace[E, Stack[E], V]
implicit def streamA[E, V <: E] = new TAReplace[E, Stream[E], V]
implicit def travA[E, V <: E] = new TAReplace[E, Traversable[E], V]
implicit def treesetA[E: Ordering, V <: E] = new TAReplace[E, TreeSet[E], V]
implicit def vectorA[E, V <: E] = new TAReplace[E, Vector[E], V]

implicit def abufferAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, ArrayBuffer[E], Repr]
implicit def hashsetAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, HashSet[E], Repr]
implicit def indexedAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, IndexedSeq[E], Repr]
implicit def iterAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Iterable[E], Repr]
implicit def listAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, List[E], Repr]
implicit def linseqAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, LinearSeq[E], Repr]
implicit def linkhsAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, LinkedHashSet[E], Repr]
implicit def linklAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, LinkedList[E], Repr]
implicit def listbAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, ListBuffer[E], Repr]
implicit def listsetAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, ListSet[E], Repr]
implicit def msetAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, MSet[E], Repr]
implicit def mlistAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, MutableList[E], Repr]
implicit def seqAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Seq[E], Repr]
implicit def setAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Set[E], Repr]
implicit def sortedsetAL[E: Ordering, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, SortedSet[E], Repr]
implicit def stackAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Stack[E], Repr]
implicit def streamAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Stream[E], Repr]
implicit def travAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Traversable[E], Repr]
implicit def treesetAL[E: Ordering, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, TreeSet[E], Repr]
implicit def vectorAL[E, Repr <: ArrayLike[E, Repr]] = new TAlikeReplace[E, Vector[E], Repr]

implicit def arrayV[Elem, V <: Elem](implicit cbf: CanBuildFrom[Array[Elem], Elem, Array[Elem]]) =
new Replace[Elem, Array[Elem], V]{
def apply(repr: Array[Elem], value: V, pred: Elem => Boolean): Array[Elem] ={
def sub(elem: Elem) = if(pred(elem)) value else elem

coll.map(sub)(cbf)
repr.map(sub)(cbf)
}
}

implicit def repTraversableSub[Elem, Repr <: TraversableLike[Elem, Repr], From <: TraversableLike[Elem, From]](implicit cbf: CanBuildFrom[Repr, Elem, Repr])=
new Replace[Elem, Repr, From] {
def apply(coll: Repr, value: From, pred: Elem => Boolean): Repr ={
implicit def arrayT[Elem, TRepr <: TraversableLike[Elem, TRepr]](implicit cbf: CanBuildFrom[Array[Elem], Elem, Array[Elem]]) =
new Replace[Elem, Array[Elem], TRepr]{
def apply(repr: Array[Elem], value: TRepr, pred: Elem => Boolean): Array[Elem] ={
val iter = value.toIterator
def sub(elem: Elem) = if(pred(elem) && iter.hasNext) iter next () else elem

coll.map(sub)(cbf)
repr.map(sub)(cbf)
}
}

implicit def repTraversableArray[Elem, Sub <: Elem, Repr <: TraversableLike[Elem, Repr], ARepr <: ArrayLike[Sub, ARepr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) =
new Replace[Elem, Repr, ARepr] {
def apply(coll: Repr, value: ARepr, pred: Elem => Boolean): Repr ={
implicit def arrayA[Elem, V <: Elem](implicit cbf: CanBuildFrom[Array[Elem], Elem, Array[Elem]]) =
new Replace[Elem, Array[Elem], Array[V]]{
def apply(repr: Array[Elem], value: Array[V], pred: Elem => Boolean): Array[Elem] ={
val iter = value.toIterator
def sub(elem: Elem) = if(pred(elem) && iter.hasNext) iter next () else elem

coll.map(sub)(cbf)
repr.map(sub)(cbf)
}
}

implicit def repArraySub[Elem, Sub <: Elem, Repr <: ArrayLike[Elem, Repr], ReprSub <: ArrayLike[Sub, ReprSub]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) =
new Replace[Elem, Repr, ReprSub] {
def apply(coll: Repr, value: ReprSub, pred: Elem => Boolean): Repr ={
implicit def arrayAL[Elem, ARepr <: ArrayLike[Elem, ARepr]](implicit cbf: CanBuildFrom[Array[Elem], Elem, Array[Elem]]) =
new Replace[Elem, Array[Elem], ARepr]{
def apply(repr: Array[Elem], value: ARepr, pred: Elem => Boolean): Array[Elem] ={
val iter = value.toIterator
def sub(elem: Elem) = if(pred(elem) && iter.hasNext) iter next () else elem

coll.map(sub)(cbf)
repr.map(sub)(cbf)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class TView[+Elem, Repr <: TraversableLike[Elem, Repr]] extends View[Elem, Repr]
def apply(coll: Repr, pred: Elem => Boolean): Repr = coll filter pred
}

class AView[Elem, Repr <: ArrayLike[Elem, Repr]] extends View[Elem, Repr] {
def apply(array: Repr, pred: Elem => Boolean): Repr = array filter pred
}

trait ViewImplicits{
implicit def abuffer[E] = new TView[E, ArrayBuffer[E]]
implicit def hashset[E] = new TView[E, HashSet[E]]
Expand Down Expand Up @@ -44,6 +40,4 @@ trait ViewImplicits{
implicit def array[E] = new View[E, Array[E]]{
def apply(a: Array[E], pred: E => Boolean) = a filter pred
}

implicit def wrapped[E] = new AView[E, WrappedArray[E]]
}

0 comments on commit 5ed7c06

Please sign in to comment.