Refactoring reducedim and related functions

This PR is along the same line as #7061, which improves the generality and coherence of the codes.

The core implementation is done in `mapreducedim!`, and everything else is just a light-weight wrapper of this function.