Scala generators, forked from gist: https://gist.github.com/arnolddevos/574873#file-gistfile1-scala