Their is quite a few functions like this already in IM core. The technique is called posterization, and if you look in wand/mogrify.c and look at what -posterize calls you will find the function it uses. (It is called PosterizeImage(), and is in "magick/quantize.c", probably with all the other simular functions)
Alturnatives including calling what the -map function calls with a pre-defined set of colors,
calling the function -colors uses to just reduce the image the best set of colors (and then calls the same map function) OR wait a bit and let me finish the new OrderedPosterizeImage() function in "threshold.c"
That last will not only reduce each channel to a specified number of 'levels' but dither the colors between those levels to generate a large number of psuedo-levels using an ordered dither pattern.
Basically, why re-invent the wheel when you have a vast range of wheels of all sorts already present
