Flatten function/operation in functional programming