Julia: Structuring code with many different but related algorithm choices
<p>I am looking for an elegant way to re-arrange my code. For developing solvers, what happens is you can have a lot of different options which have the same setup. For example, at a high level the code looks something like this:</p>

<pre><code>function solver()
  # Start by assigning a bunch of variables, preprocessing, etc.
  ...
  # Choose and execute solve
  if alg==1 doAlgorithm1()
  elseif alg==2 doAlgorithm2()
  elseif alg==3 doAlgorithm3()
  end
  # Postprocess and return
  ...
end
</code></pre>

<p>Previously when I quickly prototypes I put the solver algorithms right in the code. However, as I am tagging on more and more algorithms this is becoming messy (especially when some are hundreds of lines of code) and so I want to put those calls as a separate function. However, I want them to essentially be the same thing as putting the block of code there: access the same scope, have side effects, etc.</p>

<p>I thought about using a macro for this, but since they eval at the global scope it seemed like the wrong solution. Nested functions seem like they might be doable but I have to define them at the top of the solver (and my intention was to not do that to keep the high level algorithm readable) and there are issues with scoping a nested function within a nested function (for parts which repeat in only some of the algorithms!). I could just define this as another function without trying to keep the same scope but then it would be ugly with a long trail parameters (with each algorithm having the same parameters!)</p>

<p>What is a good way to organize this kind of code? Is there a more Julian approach to this problem?</p>