reduce function generate_string