Created
August 30, 2017 15:57
-
-
Save davidh-raybeam/4c5d3b91439b03d2931983cd6cba510e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class JoinableMap[K, V1](left: Map[K, V1]) { | |
def innerJoin[V2](right: Map[K, V2]): Map[K, (V1, V2)] = ( | |
for { | |
k <- (left.keySet & right.keySet).toSeq | |
l <- left.get(k) | |
r <- right.get(k) | |
} yield (k -> (l, r)) | |
).toMap | |
def leftJoin[V2](right: Map[K, V2]): Map[K, (V1, Option[V2])] = ( | |
for { | |
k <- left.keySet | |
l <- left.get(k) | |
} yield (k -> (l, right.get(k))) | |
).toMap | |
def rightJoin[V2](right: Map[K, V2]): Map[K, (Option[V1], V2)] = ( | |
for { | |
k <- right.keySet | |
r <- right.get(k) | |
} yield (k -> (left.get(k), r)) | |
).toMap | |
def outerJoin[V2](right: Map[K, V2]): Map[K, (Option[V1], Option[V2])] = ( | |
for { | |
k <- (left.keySet | right.keySet).toSeq | |
} yield (k -> (left.get(k), right.get(k))) | |
).toMap | |
@inline | |
def join[V2](right: Map[K, V2]): Map[K, (V1, V2)] = innerJoin(right) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment