Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 9, 2009 14:38
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 retronym/252509 to your computer and use it in GitHub Desktop.
Save retronym/252509 to your computer and use it in GitHub Desktop.
import java.util.ArrayList
object Scratch
trait Empty[+E[_]] {
def empty[A]: E[A]
}
object Emptys {
def <∅>[E[_], A](implicit e: Empty[E]): E[A] = e.empty
}
object Empty {
implicit def JavaArrayListEmpty: Empty[ArrayList] = new Empty[ArrayList] {
def empty[A] = new ArrayList[A]
}
}
trait Functor[F[_]] {
def fmap[A, B](r: F[A], f: A => B): F[B]
}
object Functor {
implicit def JavaCollectionFunctor[S[X] <: java.util.Collection[X]: Empty]: Functor[S] = new Functor[S] {
def fmap[A, B](r: S[A], f: A => B) = {
import Emptys._
val a: S[B] = <∅>[S, B]
val i = r.iterator
while (i.hasNext)
a.add(f(i.next))
a
}
}
}
implicitly[Functor[ArrayList]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment