Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 15, 2015 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/b402bdcc63c7682fad45 to your computer and use it in GitHub Desktop.
Save shigemk2/b402bdcc63c7682fad45 to your computer and use it in GitHub Desktop.
implicit def convert1(xs:Seq[Int]): {def mmap(g: Int => Int): Seq[Int]} = new{
def mmap(g: Int => Int): Seq[Int] = {
xs match {
case m if m.isEmpty => Seq()
case xs: Seq[Int] => g(xs.head) +: xs.tail.mmap(g)
}
}
}
implicit def convert2(xs:Seq[Int]): {def mfilter(g: Int => Boolean): Seq[Int]} = new{
def mfilter(g: Int => Boolean): Seq[Int] = {
xs match {
case xs if xs.isEmpty => Seq()
case xs if g(xs.head) == true => xs.head +: (xs.tail).mfilter(g)
case xs if g(xs.head) == false => (xs.tail).mfilter(g)
}
}
}
println((1 to 5).mmap((_*2)))
println((1 to 9).mfilter((x => x < 5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment