generalized mapmerge function