This is exactly the code that @josevalim suggested to me when I wrote `chunks_by` and needed to do the same thing. :p