Created
November 1, 2011 03:15
-
-
Save undetected1/1329769 to your computer and use it in GitHub Desktop.
Higher-Kinded types test
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
package com.sample.higher_kinded | |
trait Container[M[_]] { | |
def put[A](x: A): M[A] | |
def get[A](m: M[A]): A | |
} |
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
package com.sample.higher_kinded | |
object HigherKindedTest { | |
implicit val listContainer = new Container[List] { | |
def put[A](x: A) = List(x) | |
def get[A](m: List[A]) = m.head | |
} | |
implicit val optionContainet = new Container[Some] { | |
def put[A](x: A) = Some(x) | |
def get[A](m: Some[A]) = m.get | |
} | |
def tupleize[M[_]: Container, A, B](fst: M[A], snd: M[B]) = { | |
val c = implicitly[Container[M]] | |
c.put(c.get(fst), c.get(snd)) | |
} | |
def main(argc: Array[String]) = { | |
tupleize(Some(1), Some(2)) foreach println | |
tupleize(List(1), List(2)) foreach println | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment