Skip to content

Instantly share code, notes, and snippets.

@davidh-raybeam
Created August 30, 2017 15:57
Show Gist options
  • Save davidh-raybeam/4c5d3b91439b03d2931983cd6cba510e to your computer and use it in GitHub Desktop.
Save davidh-raybeam/4c5d3b91439b03d2931983cd6cba510e to your computer and use it in GitHub Desktop.
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