Factored out duplicate closure generation logic in compilation logic.

Signed-off-by: Jakob Brandsborg Ehmsen <jakobehmsen.github@gmail.com>