title: Scala: cyclomatic complexity too high for pattern matching case classes
body: <p>I have a few case classes like:</p>
  <pre><code>case class TypeX(a: Int, b: Int, c: String)
  sealed trait metrictypes
  object metrictypes {
    case class SuccessCount(a: TypeX) extends metrictypes
    case class FailureCount(a: TypeX) extends metrictypes
    case class WarningCount(a: TypeX) extends metrictypes
       .....
  </code></pre>
  <p>I have 10 such case classes ^</p>
  <p>I have some functionality which results in a list <code>collectedList</code> of type List[metrictypes]. For example:</p>
  <p><code>List(SuccessCount(TypeX(111, 222, &quot;abc&quot;)), WarningCount(TypeX(999, 777, &quot;zzz&quot;)))</code></p>
  <p>I need to count metrictype and also collect TypeX in a list <code>finalList</code>. I'm doing something like:</p>
  <pre><code>  val finalList = new ListBuffer[TypeX]()
    val result = collectedList {x =&gt;
      x match {
        case SuccessCount(a)  =&gt; incrementSuccessCount(); finalList += a
        case FailureCount(a)  =&gt; incrementFailureCount(); finalList += a
        .... (10 times)
        case _ =&gt; println(&quot;Unknown!&quot;)
  
      }
    }
  </code></pre>
  <p>I know this isn't the cleanest or efficient way to do it and it also results in a high cyclomatic complexity. How can I improve this?</p>
  <p>I also tried grouping them based on the metrictypes by doing:</p>
  <pre><code>  val groupedList = collectedList.groupBy(i =&gt; i).mapValues(_.map(_ =&gt; 1).reduce(_ + _))
  </code></pre>
  <p>This results in</p>
  <pre><code>Map(SuccessCount(TypeX(111, 222, &quot;abc&quot;)) -&gt; 1, WarningCount(TypeX(999, 777, &quot;zzz&quot;)) -&gt; 1))
  </code></pre>
  <p>Still unable to figure out how I can get the counts of occurrences for metrictypes AND collect TypeX only in a list.</p>