Optimized `foldl` and `foldM`