fold_map implementation

A convenience function to lazily map and fold a generator.