title: What's the "functional way" to avoid passing state-choosing context down the call stack?
body: <p>Say I have a trait that has two lists. Sometimes I'm interested in the one, sometimes in t'other.</p>
  
  <pre><code>trait ListHolder {
    val listOne = List("foo", "bar")
    val listTwo = List("bat", "baz")
  }
  </code></pre>
  
  <p>I have a chain of function calls, at the top of which I have the context I need to choose between the lists, but at the bottom of which I use the trait. </p>
  
  <p>In the imperative paradigm, I pass down the context through the functions:</p>
  
  <pre><code>class Imperative extends Object with ListHolder {
    def activeList(choice : Int) : List[String] = {
      choice match {
        case 1 =&gt; listOne
        case 2 =&gt; listTwo
      }
    }
  }
  
  def iTop(is : List[Imperative], choice : Int) = {
    is.map{iMiddle(_, choice)}
  }
  
  def iMiddle(i : Imperative, choice : Int) = {
    iBottom(i, choice)
  }
  
  def iBottom(i : Imperative, choice : Int) = {
    i.activeList(choice)
  }
  
  val ps = List(new Imperative, new Imperative)
  println(iTop(ps, 1)) //Prints "foo, bar" "foo,bar"
  println(iTop(ps, 2)) //Prints "bat, baz" "bat, baz"
  </code></pre>
  
  <p>In the object-oriented paradigm, I can use internal state to avoid passing the context down:</p>
  
  <pre><code>class ObjectOriented extends Imperative {
    var variable = listOne
  }
  
  def oTop(ps : List[ObjectOriented], choice : Int) = {
    ps.map{ p =&gt; p.variable = p.activeList(choice) }
    oMiddle(ps)
  }
  
  def oMiddle(ps : List[ObjectOriented]) = oBottom(ps)
  
  def oBottom(ps : List[ObjectOriented]) = {
    ps.map(_.variable)  //No explicitly-passed-down choice, but hidden state
  }
  
  val oops = List(new ObjectOriented, new ObjectOriented)
  
  println(oTop(oops, 1))
  println(oTop(oops, 2))
  </code></pre>
  
  <p>What is the idiomatic way to achieve a similar outcome in a functional language? </p>
  
  <p>That is, I would like the output of the following to be similar to the output from the above.</p>
  
  <pre><code>class Functional extends Object with ListHolder{
    //IDIOMATIC FUNCTIONAL CODE
  }
  
  def fTop(fs : List[Functional], choice : Int) = {
      //CODE NEEDED HERE TO CHOOSE LIST
      fMiddle(fs)
  }
  
  def fMiddle(fs : List[Functional]) = {
     //NO CHANGES ALLOWED
     fBottom(fs)
  }
  
  def fBottom(fs : List[Functional]) = {
    fs.map(_.activeList) //or similarly simple
  }
  
  def fs = List(new Functional, new Functional)
  
  println(fTop(fs, 1))
  println(fTop(fs, 2))
  </code></pre>
  
  <p>UPDATE: 
  Would this be considered properly functional? </p>
  
  <pre><code>class Functional extends Imperative with ListHolder{}
  
  class FunctionalWithList(val activeList : List[String]) extends Functional{}
  
  def fTop(fs : List[Functional], band : Int) = {
    fMiddle(fs.map(f =&gt; new FunctionalWithList(f.activeList(band))))
  }
  
  def fMiddle(fs : List[FunctionalWithList]) = {
    //NO CHANGES ALLOWED
    fBottom(fs)
  }
  
  def fBottom(fs : List[FunctionalWithList]) = {
    fs.map(_.activeList)
  }
  
  def fs = List(new Functional, new Functional)
  
  println(fTop(fs, 1))
  println(fTop(fs, 2))
  </code></pre>