Created
December 9, 2009 14:38
-
-
Save retronym/252509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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