title: scala coding-style: initialize a val Seq depending on optional values
body: <p>I'm developing a simple function that should build a sql condition based on optional parameters that might not be present.</p>
  
  <p>I'm trying to accomplish it using immutables values, and in and elegant and idiomatic way.</p>
  
  <p>So far now I came with something like this:</p>
  
  <pre><code>val cond: Seq[String] = Nil ++ 
    (if (entity != "*") Seq("entity = {entity}") else Nil) ++
    (if (name != "*") Seq("name = {name}") else Nil) ++
  
  val condition = 
    if (cond.size == 0) "" 
    else " where " + cond.mkString(" and ") + " "
  </code></pre>
  
  <p>So, I'm just mergins Seqs, if the value is not present I'm merging it with Nil</p>
  
  <p>Working with a var I get something like:</p>
  
  <pre><code>var cond: Seq[String] = Seq();
  if (entity != "*") cond = cond :+ "entity = {entity}"
  if (name != "*") cond = cond :+ "name = {name}"
  </code></pre>
  
  <p>Which I find it to be more readable (I wonder if this second approach is not threadsafe because of the var)</p>
  
  <p>I wanted to know what would be a scala idiomatic and elegant way to solve such a thing</p>
  
  <p>-- edit</p>
  
  <p>in the end, I settled down with this implementation:</p>
  
  <pre><code>val condition: String = {
  
    var cond: Seq[String] = Seq();
  
    if (entity != "") 
      cond = cond :+ "entity = {entity}"
    if (filter != "")
      cond = cond :+ """(name like {filter} or prev like {filter} or post like {filter})"""
  
    if (cond.size == 0) "" 
    else " where " + cond.mkString(" and ") + " "
  }
  </code></pre>