title: spark - stack multiple when conditions from an Array of column expressions
body: <p>I have the below spark dataframe:</p>
  <pre><code>val df = Seq((&quot;US&quot;,10),(&quot;IND&quot;,20),(&quot;NZ&quot;,30),(&quot;CAN&quot;,40)).toDF(&quot;a&quot;,&quot;b&quot;)
  df.show(false)
  +---+---+
  |a  |b  |
  +---+---+
  |US |10 |
  |IND|20 |
  |NZ |30 |
  |CAN|40 |
  +---+---+
  </code></pre>
  <p>and I'm applying the <code>when()</code> condition as follows:</p>
  <pre><code>df.withColumn(&quot;x&quot;, when(col(&quot;a&quot;).isin(us_list:_*),&quot;u&quot;).when(col(&quot;a&quot;).isin(i_list:_*),&quot;i&quot;).when(col(&quot;a&quot;).isin(n_list:_*),&quot;n&quot;).otherwise(&quot;-&quot;)).show(false)
  
  +---+---+---+
  |a  |b  |x  |
  +---+---+---+
  |US |10 |u  |
  |IND|20 |i  |
  |NZ |30 |n  |
  |CAN|40 |-  |
  +---+---+---+
  </code></pre>
  <p>Now to minimize the code, I'm trying the below:</p>
  <pre><code>val us_list = Array(&quot;U&quot;,&quot;US&quot;)
  val i_list = Array(&quot;I&quot;,&quot;IND&quot;)
  val n_list = Array(&quot;N&quot;,&quot;NZ&quot;)
  val ar1 = Array((us_list,&quot;u&quot;),(i_list,&quot;i&quot;),(n_list,&quot;n&quot;))
  
  val ap = ar1.map( x =&gt; when(col(&quot;a&quot;).isInCollection(x._1),x._2) )
  </code></pre>
  <p>This results in</p>
  <pre><code>ap: Array[org.apache.spark.sql.Column] = Array(CASE WHEN (a IN (U, US)) THEN u END, CASE WHEN (a IN (I, IND)) THEN i END, CASE WHEN (a IN (N, NZ)) THEN n END)
  </code></pre>
  <p>but when I try</p>
  <pre><code>val ap = ar1.map( x =&gt; when(col(&quot;a&quot;).isInCollection(x._1),x._2) ).reduce( (x,y) =&gt; x.y )
  </code></pre>
  <p>I get an error. How to fix this?</p>