Skip to content

Instantly share code, notes, and snippets.

@hyleung
Last active August 29, 2015 14:23
Show Gist options
  • Save hyleung/c2a6df9c44a91b563926 to your computer and use it in GitHub Desktop.
Save hyleung/c2a6df9c44a91b563926 to your computer and use it in GitHub Desktop.
Map transformation - combine two maps
def groupMaps[A,B,C](m:(A,Map[B,C]))(n:(A,Map[B,C])):Map[B,Map[A,C]] = {
(m,n) match {
case ((a,mm),(b,nn)) if mm.nonEmpty && nn.nonEmpty =>
Map(mm.head._1 -> Map(a -> mm.head._2, b -> nn.head._2)) ++ groupMaps((a, mm.tail))((b, nn.tail))
case ((a,_),(b,_)) => Map.empty[B,Map[A,C]]
}
}
val hostA = ("hostA", Map("/oauth2/token" -> "1", "/carts/default" -> "2"))
val hostB = ("hostB", Map("/oauth2/token" -> "3", "/carts/default" -> "4"))
val host1 = ("hostA", Map("/oauth2/token" -> 1, "/carts/default" -> 2))
val host2 = ("hostB", Map("/oauth2/token" -> 3, "/carts/default" -> 4))
groupMaps(hostA)(hostB)
groupMaps(host1)(host2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment