title: Scala conditional list construction
body: <p>I'm using Scala 2.9.2, and would like to construct a list based on some conditions.</p>
  
  <p>Consider the following, where cond is some function taking a predicate p and a value of type T (in this case t3):</p>
  
  <pre><code>t1 :: t2 :: cond( p, t3 ) :: t4
  </code></pre>
  
  <p>The behaviour I want is as follows. If p is true, this should give:</p>
  
  <pre><code>List[T]( t1, t2, t3, t4 )
  </code></pre>
  
  <p>If p evaluates to false, this should give:</p>
  
  <pre><code>List[T]( t1, t2, t4 )
  </code></pre>
  
  <p>I'm probably thinking about this completely the wrong way, but I'm struggling to come up with an elegant solution. I could involve Options everywhere and then filter, but that's makes the code rather harder to read:</p>
  
  <pre><code>def cond[T]( p : =&gt; Boolean, v : T ) : Option[T] =
  {
      p match
      {
          case true =&gt; Some( v )
          case false =&gt; None
      }
  }
  </code></pre>
  
  <p>This allows the following:</p>
  
  <pre><code>scala&gt; ( Some( 1 ) :: Some( 2 ) :: cond( true, 3 ) :: Some( 4 ) :: Nil ).flatten
  res4: List[Int] = List(1, 2, 3, 4)
  
  scala&gt; ( Some( 1 ) :: Some( 2 ) :: cond( false, 3 ) :: Some( 4 ) :: Nil ).flatten
  res5: List[Int] = List(1, 2, 4)
  </code></pre>
  
  <p>However, it's not the most elegant solution, as it requires the user to wrap all of the their non-conditional elements in Some( ) and also to remember to do the flatten at the end. Can anyone think of a more elegant solution?</p>