Created
May 8, 2012 16:22
-
-
Save kaja47/2636926 to your computer and use it in GitHub Desktop.
ScalaZ: Map bifunctor
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
implicit def MapBifunctor: Bifunctor[Map] = new Bifunctor[Map] { | |
def bimap[A, B, C, D](k: Map[A, B], f: A => C, g: B => D) = | |
k map { case (k, v) => (f(k), g(v)) } // k map { f <-: _ :-> g } | |
} | |
// ordinary bifunctor on pairs | |
val add100 = (x: Int) => x + 100 | |
(1, 1) :-> add100 // (1, 101) | |
add100 <-: (1, 1) // (101, 1) | |
add100 <-: (1, 1) :-> add100 // (101, 101) | |
// my bifunctor on maps | |
val m = Map(1 -> 1, 2 -> 2, 3 -> 3) | |
m :-> add100 // Map( 1 -> 101, 2 -> 102, 3 -> 103) | |
add100 <-: m // Map(101 -> 1, 102 -> 2, 103 -> 3) | |
add100 <-: m :-> add100 // Map(101 -> 101, 102 -> 102, 103 -> 103) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment