Skip to content

Instantly share code, notes, and snippets.

@notyy
Created October 26, 2012 11:52
Show Gist options
  • Save notyy/3958354 to your computer and use it in GitHub Desktop.
Save notyy/3958354 to your computer and use it in GitHub Desktop.
implicit list as functor
trait Functor[A, T[_]] {
def fmap[B](f: A => B): T[B]
}
class ListFunctor[A](val list: List[A]) extends Functor[A, List] {
override def fmap[B](f: A => B): List[B] = list match {
case List() => List()
case (x :: xs) => f(x) :: xs.fmap(f)
}
}
implicit def listToFunctor[A](list: List[A]): ListFunctor[A] = new ListFunctor(list)
/////////
List(1,2,3).fmap(_.toString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment