Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving towards a more Lens like structure
- Loading branch information
Showing
7 changed files
with
83 additions
and
204 deletions.
There are no files selected for viewing
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
68 changes: 0 additions & 68 deletions
68
src/main/scala/com/wheaties/choice/iteration/IterationScheme.scala
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.wheaties.choice.iteration | ||
|
||
import scala.collection.TraversableLike | ||
import scala.collection.generic._ | ||
|
||
trait Modify[Elem, Collection] extends ((Collection, Elem => Elem, Elem => Boolean) => Collection) | ||
|
||
trait ModifyImplicits{ | ||
implicit def modTraversable[Elem, Repr <: TraversableLike[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 | ||
|
||
coll.map(sub)(cbf) | ||
} | ||
} | ||
} |
72 changes: 45 additions & 27 deletions
72
src/main/scala/com/wheaties/choice/iteration/Replace.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,55 @@ | ||
package com.wheaties.choice.iteration | ||
|
||
//TODO: change to implicit object that creates functions | ||
import scala.collection.TraversableLike | ||
import scala.collection.generic._ | ||
|
||
trait Replace[Elem,+Action] extends (Elem => Elem){ | ||
def apply(elem: Elem): Elem | ||
} | ||
trait Replace[Elem,Collection,Sub] extends ((Collection, Sub, Elem => Boolean) => Collection) | ||
|
||
class Substitute[Elem](value: Elem) extends Replace[Elem,Elem]{ | ||
def apply(elem: Elem) = value | ||
} | ||
//TODO: Missing mutable... | ||
trait ReplaceImplicits{ | ||
implicit def repTraversable[Elem, Repr <: TraversableLike[Elem, Repr]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) = | ||
new Replace[Elem, Repr, Elem] { | ||
def apply(coll: Repr, value: Elem, pred: Elem => Boolean): Repr ={ | ||
def sub(elem: Elem) = if(pred(elem)) value else elem | ||
|
||
class SubIterable[Elem,To,I[To] <: Iterable[Elem]](value: I[To]) extends Replace[Elem,I[To]]{ | ||
private val iter = value.iterator | ||
coll.map(sub)(cbf) | ||
} | ||
} | ||
|
||
def apply(elem: Elem) = if(iter hasNext) iter next () else elem | ||
} | ||
implicit def repTraversable[Elem, Array[_]] = new Replace[Elem, Array[Elem], Elem] { | ||
def apply(coll: Array[Elem], value: Elem, pred: Elem => Boolean): Array[Elem] ={ | ||
def sub(elem: Elem) = if(pred(elem)) value else elem | ||
|
||
class SubStream[Elem,To,S[To] <: Stream[Elem]](var value: S[To]) extends Replace[Elem,S[To]]{ | ||
def apply(elem: Elem) = value match{ | ||
case head #:: tail => value = tail; head | ||
case _ => elem | ||
coll map sub | ||
} | ||
} | ||
} | ||
|
||
class SubArray[Elem : ClassManifest, To, A[To] <: Array[Elem]](value: A[To]) extends Replace[Elem,A[To]]{ | ||
private val iter = value.iterator | ||
|
||
def apply(elem: Elem) = if(iter hasNext) iter next () else elem | ||
} | ||
|
||
class SubKeys[Key, To, Value, C[To] <: Iterable[Key]](value: C[To]) | ||
extends Replace[(Key,Value),C[To]]{ | ||
private val iter = value.iterator | ||
|
||
def apply(elem: (Key,Value)) = if(iter hasNext) (iter next (), elem._2) else elem | ||
implicit def repTraversableSub[Elem, Sub <: Elem, Repr <: TraversableLike[Elem, Repr], From <: TraversableLike[Sub, From]](implicit cbf: CanBuildFrom[Repr, Elem, Repr])= | ||
new Replace[Elem, Repr, From] { | ||
def apply(coll: Repr, value: From, pred: Elem => Boolean): Repr ={ | ||
val iter = value.toIterator | ||
def sub(elem: Elem) = if(pred(elem) && iter.hasNext) iter next () else elem | ||
|
||
coll.map(sub)(cbf) | ||
} | ||
} | ||
|
||
implicit def repTraversableArray[Elem, Sub <: Elem, Repr <: TraversableLike[Elem, Repr], Array[_]](implicit cbf: CanBuildFrom[Repr, Elem, Repr]) = | ||
new Replace[Elem, Repr, Array[Sub]] { | ||
def apply(coll: Repr, value: Array[Sub], pred: Elem => Boolean): Repr ={ | ||
val iter = value.toIterator | ||
def sub(elem: Elem) = if(pred(elem) && iter.hasNext) iter next () else elem | ||
|
||
coll.map(sub)(cbf) | ||
} | ||
} | ||
|
||
implicit def repArraySub[Elem, Sub <: Elem, Array[_]] = new Replace[Elem, Array[Elem], Array[Sub]] { | ||
def apply(coll: Array[Elem], value: Array[Sub], 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 | ||
} | ||
} | ||
} |
93 changes: 0 additions & 93 deletions
93
src/main/scala/com/wheaties/choice/iteration/Traverse.scala
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.wheaties.choice.iteration | ||
|
||
import scala.collection.TraversableLike | ||
|
||
trait View[Elem,Collection] extends ((Collection, Elem => Boolean) => Collection) | ||
|
||
trait ViewImplicits{ | ||
implicit def viewTraversable[Elem, Repr <: TraversableLike[Elem, Repr]] = new View[Elem, Repr] { | ||
def apply(coll: Repr, pred: Elem => Boolean): Repr = coll filter pred | ||
} | ||
|
||
implicit def viewArray[Elem, Array[_]] = new View[Elem, Array[Elem]] { | ||
def apply(array: Array[Elem], pred: Elem => Boolean): Array[Elem] = array filter pred | ||
} | ||
|
||
//implicit def viewArray[Elem, Repr <: ArrayLike[Elem, Repr]] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package com.wheaties.predicate | ||
|
||
package object predicate{ | ||
def not(value: Boolean) = !value | ||
implicit def sugarIs[A](value: A) = new{ def is(pred: A => Boolean) = pred(value) } | ||
} |