Skip to content

Instantly share code, notes, and snippets.

@mathhun
Created July 13, 2016 06:55
Show Gist options
  • Save mathhun/801b2e411cda594692fb42a2f1dbfef7 to your computer and use it in GitHub Desktop.
Save mathhun/801b2e411cda594692fb42a2f1dbfef7 to your computer and use it in GitHub Desktop.
Map.scala
object Main extends App {
// 似非 package
object syntax { object std { object map {
final class MapOps[K, V](self: Map[K, V]) {
final def connect[W](other: Map[V, W]): Map[K, W] =
self flatMap { case (k, v) => other.get(v) map (k -> _) }
}
implicit def ToMapOps[K, V](m: Map[K, V]): MapOps[K, V] = new MapOps[K, V](m)
} } }
import syntax.std.map._
val a = Map('a -> 1, 'b -> 2, 'c -> 3)
val b = Map(1 -> "a", 2 -> "b")
val c = Map("a" -> "A", "b" -> "B")
val z = a.connect(b).connect(c)
println(z)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment