Skip to content

Instantly share code, notes, and snippets.

@rahilb
Created November 25, 2015 16:26
Show Gist options
  • Save rahilb/372d8e34e1669013c9c8 to your computer and use it in GitHub Desktop.
Save rahilb/372d8e34e1669013c9c8 to your computer and use it in GitHub Desktop.
Functor Abstraction Pattern
import cats.Functor
trait ImplicitInstances {
implicit val OptionInstance: Functor[Option] = new Functor[Option] {
override def map[A, B](fa: Option[A])(f: (A) => B): Option[B] = fa.map(f)
}
}
final class FuncAbs[M[_]] {
def double(s: M[String])(implicit functor : Functor[M]) = functor.map(s)(str => str + str)
}
object Main extends App with ImplicitInstances {
val opt = new FuncAbs[Option]
val dub = opt.double(Some("foo"))
println(dub)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment