Refactor to functional thinking code by using transpose and map