Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created November 2, 2015 00:19
Show Gist options
  • Select an option

  • Save dbousamra/cefd8bd52aa293adebcc to your computer and use it in GitHub Desktop.

Select an option

Save dbousamra/cefd8bd52aa293adebcc to your computer and use it in GitHub Desktop.
trait Replaceable[A] {
def replace[A, B](src: A)(in: A, out: B): B
}
trait StringExample extends Replaceable[String] {
def replace(src: String)(in: String, out: String): String = {
src.replace(in, out)
}
}
trait ListExample[A, B] extends Replaceable[List[A]] {
private def replacer(x: A, y: B) = (i: A) => if (i == x) y else i
def replace(src: List[A])(in: A, out: B): List[B] = {
src map(item => replacer(in, out)(item))
}
}
trait FutureExample[A, B] extends Replaceable[Future[A]] {
private def replacer(x: A, y: B) = (i: A) => if (i == x) y else i
def replace(src: Future[A])(in: A, out: B): List[B] = {
src map(item => replacer(in, out)(item))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment