I just grabbed the first part of code that is very easy to understand. I didn't know that changing the order of yielded values doesn't make any difference for you (I did think about how greatly it would simplify transform but forgot when posting).
I only had a glance at the rest of the functions. Separately, they don't look that trivial to optimize heavily but that's because I didn't grasp the whole idea of the program (I was quite sleepy when I posted my answer). But I guess you get the general idea now.