reduce function implemented