Replacing an each with reduce

The functionality has not changed, but the code is more elegant by
using `reduce` instead of `each`.

This way no accumulator needs to be declared, no explicit return is
needed.
Using `each_with_object` instead of `reduce`

This way no new object allocation is taking place. Thanks @jeremy for
the suggestion!
Merge pull request #16626 from adomokos/replacing-each-with-reduce

Replacing an each with reduce