title: scala: memoize a function no matter how many arguments the function takes?
body: <p>i want to write a memoize function in scala that can be applied to any function object no matter what that function object is. i want to do so in a way that lets me use a single implementation of memoize. i'm flexible about the syntax, but ideally the memoize appears somewhere very close to the declaration of the function as opposed to after the function. i'd also like to avoid first declaring the original function and then a second declaration for the memoized version.</p>
  
  <p>so some ideal syntax might be this:</p>
  
  <pre><code>def slowFunction(&lt;some args left intentionally vague&gt;) = memoize {
    // the original implementation of slow function
  }
  </code></pre>
  
  <p>or even this would be acceptable:</p>
  
  <pre><code>def slowFUnction = memoize { &lt;some args left intentionally vague&gt; =&gt; {
    // the original implementation of slow function
  }}
  </code></pre>
  
  <p>i've seen ways to do this where memoize must be redefined for each arity function, but i want to avoid this approach. the reason is that i will need to implement dozens of functions similar to memoize (i.e. other decorators) and it's too much to ask to have to copy each one for each arity function.</p>
  
  <p>one way to do memoize that does require you to repeat memoize declarations (so it's no good) is at <a href="https://stackoverflow.com/questions/3640823/in-scala-2-8-what-type-to-use-to-store-an-in-memory-mutable-data-table/3640889#3640889">What type to use to store an in-memory mutable data table in Scala?</a>.</p>