Proposal: mapRight

Tbh I would have expected a such-named method to still return the results in the normal order, and just to process the elements in the reverse order (from the right). But that's generally a bit pointless, as map is expected to be order-independent (sans exceptions, maybe).

I think your use case should rather be solved using reverse iteration, see Use Cases 路 Issue #1 路 tc39/proposal-reverseIterator 路 GitHub / GitHub - leebyron/ecmascript-reverse-iterable: ReverseIterable Interface Spec Proposal / Array.prototype.reversed() (+Map, Set). These would allow you to write something like

const arr = [1, 2, 3, 4, 5];
const result = arr.reversedValues().map(x => x + 1).toArray(); // [6,5,4,3,2]
// or
const result = arr.values().reverse().map(x => x + 1).toArray(); // [6,5,4,3,2]

which is much easier to understand imo, using the familiar term "reverse(d)" instead of "right".

1 Like