title: Scala map with dependent variables
body: <p>In scala I have a list of functions that return a value. The order in which the functions are executed are important since the argument of function <code>n</code> is the output of function <code>n-1</code>.</p>
  
  <p>This hints to use <code>foldLeft</code>, something like:</p>
  
  <pre><code>val base: A
  val funcs: Seq[Function[A, A]]
  
  funcs.foldLeft(base)(x, f) =&gt; f(x)
  </code></pre>
  
  <p>(detail: type <code>A</code> is actually a Spark DataFrame).</p>
  
  <p>However, the results of each functions are mutually exclusive and in the end I want the union of all the results for each function.
  This hints to use a <code>map</code>, something like:</p>
  
  <pre><code>funcs.map(f =&gt; f(base)).reduce(_.union(_)
  </code></pre>
  
  <p>But here each function is applied to <code>base</code> which is not what I want.</p>
  
  <p>Short: A list of variable length of ordered functions needs to return a list of equal length of return values, where each value <code>n-1</code> was the input for function <code>n</code> (starting from <code>base</code> where <code>n=0</code>). Such that the result values can be concatenated.</p>
  
  <p>How can I achieve this?</p>
  
  <p><strong>EDIT</strong>
  example:</p>
  
  <pre><code>case class X(id:Int, value:Int)
  val base = spark.createDataset(Seq(X(1, 1), X(2, 2), X(3, 3), X(4, 4), X(5, 5))).toDF
  
  def toA = (x: DataFrame) =&gt; x.filter('value.mod(2) === 1).withColumn("value", lit("a"))
  def toB = (x: DataFrame) =&gt; x.withColumn("value", lit("b"))
  
  val a = toA(base)
  val remainder = base.join(a, Seq("id"), "leftanti")
  val b = toB(remainder)
  
  a.union(b)
  
  +---+-----+
  | id|value|
  +---+-----+
  |  1|    a|
  |  3|    a|
  |  5|    a|
  |  2|    b|
  |  4|    b|
  +---+-----+
  </code></pre>
  
  <p>This should work for an arbitrary number of functions (e.g. <code>toA</code>, <code>toB</code> ... <code>toN</code>. Where each time the remainder of the previous result is calculated and passed into the next function. In the end a union is applied to all results.</p>