title: Pattern matching against Scala Map entries
body: <p>Is there any Scala trick to enable pattern matching against map keys? In other words, I'd like to have an extractor that beside the Map instance accepted also a key value that would mean I want this pattern to match only if the matchable value is an instance of Map and there is an entry with the given key in it and the value for this entry be subject to recursive pattern matching.</p>
  
  <p>Something like this:</p>
  
  <pre><code>myMap match {
      case MyMap("a")(a) =&gt; // do smth with value a
      case MyMap("b")(MyMap("c")(c)) =&gt; // do smth with value c
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I've found some way to approach closer to the goal, but it's still not perfect because it implies definition of synthetic key-value-holders:</p>
  
  <pre><code>case class MapKey[K](key: K) {
    def unapply(o: Any) = o match {
      case m: Map[K, _] ⇒ m.get(key)
      case _ ⇒ None
    }
  }
  
  val m1 = Map("a" → "aa", "b" → Map("c" → "cc"))
  val m2 = Map("a" → "aa", "d" → "dd")
  
  val b = MapKey("b")
  val c = MapKey("c")
  val d = MapKey("d")
  
  for (m ← List(m1, m2)) m match {
    case b(c(x)) ⇒ println(s"b &gt; c: $x")
    case d(x) ⇒ println(s"d: $x")
  }
  </code></pre>
  
  <p>Similar question: <a href="https://stackoverflow.com/questions/2411573/can-extractors-be-customized-with-parameters-in-the-body-of-a-case-statement-or">Can extractors be customized with parameters in the body of a case statement (or anywhere else that an extractor would be used)?</a></p>
  
  <p>Feature request: <a href="https://issues.scala-lang.org/browse/SI-5435" rel="nofollow noreferrer">SI-5435</a></p>