refactor reduce (foldLeft) implementation.
add countBy.