Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active December 12, 2015 04:48
Show Gist options
  • Save wheaties/4716441 to your computer and use it in GitHub Desktop.
Save wheaties/4716441 to your computer and use it in GitHub Desktop.
Working out some ideas.
trait Mapper[Elem,C[_]]{
def onTrue(t: Elem => Elem): FilterTraversable[Elem,C]
def onFalse(f: Elem => Elem): FilterTraversable[Elem,C]
def on[A](f: Elem => Elem, t: Elem => Elem): FilterTraversable[Elem,C]
def apply(collection: C[Elem], condition: Elem => Boolean): C[Elem]
}
case class MapperIterable[Elem,I[_] <: Iterabable[_]](t: Elem => Elem, f: Elem => Elem) extends Mapper[Elem,I]{
def onTrue(func: Elem => Elem) = copy(t = func)
def onFalse(func: Elem => Elem) = copy(f = func)
def on[A](funcFalse: Elem => Elem, funcTrue: Elem => Elem) = copy(f = funcFalse, t = funcTrue)
def apply(collection: I[Elem], condition: Elem => Boolean): I[Elem] ={
collection map(elem => if(condition(elem)) t(elem) else f(elem))
}
}
trait Replace[Elem,Action]{
type Out
def apply(elem: Elem): Out
}
class Substitute[@specialized(Int,Long,Double,Float,Boolean) Elem,
@specialized(Int,Long,Double,Float,Boolean) To](value: To) extends Replace[Elem,To]{
type Out = To
def apply(x: Elem) = value
}
class SubstituteIterable[@specialized(Int,Long,Double,Float,Boolean) Elem,
@specialized(Int,Long,Double,Float,Boolean) To <: Elem,
I[_] <: Iterable[_]](values: I[To]) extends Replace[Elem,I[To]]{
private val iter = values toIterator
type Out = Elem
def apply(x: Elem) = if(iter hasNext) iter next () else x
}
class MapReplace[@specialized(Int,Long,Double,Float,Boolean) Elem,
@specialized(Int,Long,Double,Float,Boolean) To <: Elem](f: Elem => To) extends Replace[Elem,Elem => To]{
type Out = To
def apply(x: Elem) = f(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment