title: Collapse similar case statements in Scala
body: <p>Is there an elegant way to do something like the following example using just one case statement?</p>
  
  <pre><code>foobar match {
      case Node(Leaf(key, value), parent, qux) =&gt; {
          // Do something with parent &amp; qux
      }
      case Node(parent, Leaf(key, value), qux) =&gt; {
          // Do something with parent &amp; qux (code is the same as in the previous case)
      }
      // other cases
  }
  </code></pre>
  
  <p>For an understanding of what is going on here: <code>foobar</code> is a node of a binary tree, and I match the cases when one of the node's ancestors is a <code>Leaf</code> node. These are the classes used:</p>
  
  <pre><code>abstract class Tree
  case class Node(left: Tree, right: Tree, critBit: Int) extends Tree
  case class Leaf(key: String, value:String) extends Tree
  </code></pre>