title: Scala function that return another function which can internally hold data
body: <p>For simplicity, let's say I want to create a function as such:</p>
  
  <pre><code>def powerOfTwo(n: Int): Int =&gt; Int
  </code></pre>
  
  <p>The output is another function that can be used as such that outputFunction(x) will give me 2^x.</p>
  
  <p>the <code>n: Int</code> indicates the range of powers of 2 from 1 to n, that is <code>{2^1, 2^2, ... 2^n}</code>. I want these values to be pre-computed such that the resulting function <code>Int =&gt; Int</code> can be used by a simple lookup to obtain the corresponding element of that Array of powers of 2.</p>
  
  <p>I wonder how can I accomplish that? Should I be making a <code>Class</code> which I then use to initialize some internal <code>var</code> object, and then pass around a function which will output the corresponding element of that <code>var</code> object?</p>
  
  <p>Hope this is clear. Note that power of 2 is just an example. I am interested in how to implement this idea?</p>