Skip to content

Instantly share code, notes, and snippets.

@caente
Created May 15, 2015 19:23
Show Gist options
  • Save caente/8269ac39e5ae765f3a05 to your computer and use it in GitHub Desktop.
Save caente/8269ac39e5ae765f3a05 to your computer and use it in GitHub Desktop.
implicit class TaggedMonad[A, B](val t:A @@ B ) extends AnyVal {
def map[C](f: A => C ):C @@ B = Tag[C, B](f(Tag.unwrap(t)))
def flatMap[C](f : A => C @@ B): C @@ B =f(Tag.unwrap(t))
}
sealed trait A
sealed trait B
val i: Int @@ A = Tag[Int, A](1)
val s: String @@ A = i.map(_.toString)
val a: String @@ A = i.flatMap(n => Tag[String, A](n.toString))
val b: Int @@ B @@ A = i.map(n => Tag[Int,B](n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment