Keyword classification in Scala... simple? I am pretty newbie in Scala, my background expertise is basically Java and Python, and I'd like to know your thoughts about this code snippet written in Scala to know if this is a &quot;regular piece of code written in Scala&quot; or if this is something kind of &quot;obfuscated&quot; and overcomplicated.
The code just implements a simple decision flow to categorize keywords depending on a few rules.
Thanks!
def preprocess(kwd: Keyword): Option[PrepKeyword] = {

val isFoo = kwd.name.exists(_.toLowerCase(locale) == &quot;foo&quot;)
    for {
      filterCategory &lt;- (isFoo, kwd.type) match {
                case (true, _)  =&gt; Some(false)
                case (false, 1) =&gt; Some(true)
                case (false, 2) =&gt; Some(false)
                case _          =&gt; None
              }
      fooName &lt;- kwd.name match {
                        case None | Some(&quot;name1&quot;)                           =&gt; None
                        case Some(&quot;myCategory&quot;) if filterCategory           =&gt; Some(&quot;GreatCategory&quot;)
                        case other                                          =&gt; other
                      }
    } yield PrepKeyword(
      keyword = kwd,
      name = fooName,
      path = if (isFoo) &quot;Foo1&quot; else &quot;Foo2&quot;
    )
  }
The posted code won't compile without a working definition of Keyword and PrepKeyword. And type is a reserved word so kwd.type isn't allowed (might be version dependent).
The code, as presented, is inconsistent or just plain wrong in its indenting but, other than that, it's reasonably clear in intent and presentation.
The logic is, perhaps, overly convoluted. I found that I had to go back and forth to see how output was built from input. I eventually decided that kwd.name was the key value from which most everything else flows. With that in mind I tried to rebuild the logic.
def preprocess(kwd: Keyword): Option[PrepKeyword] = {
  val fooName = &quot;(?i)(foo)&quot;.r
  kwd.name.flatMap{
    case &quot;name1&quot;      =&gt; None
    case &quot;myCategory&quot; =&gt;
      kwd.typ match {
        case 1 =&gt; Some(PrepKeyword(kwd, &quot;GreatCategory&quot;, &quot;Foo2&quot;))
        case 2 =&gt; Some(PrepKeyword(kwd, &quot;myCategory&quot;, &quot;Foo2&quot;))
        case _ =&gt; None
      }
    case fooName(fnm) =&gt; Some(PrepKeyword(kwd, fnm, &quot;Foo1&quot;))
    case nonFoo       =&gt; Some(PrepKeyword(kwd, nonFoo, &quot;Foo2&quot;))
  }
}

This, according to my tests so far, produces the same results. It builds the output in multiple places but in each case it's pretty easy to see how the input directed the output.