Created
July 13, 2016 06:55
-
-
Save mathhun/801b2e411cda594692fb42a2f1dbfef7 to your computer and use it in GitHub Desktop.
Map.scala
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
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