I’ve created a PR to add some functions to Belt.Array, mostly reverse functions of forEach, map, etc. In order not to bloat the API surface of Belt with mapReverse, forEachReverse etc, Hongbo suggests instead to add a flag to the existing functions.
let map: (array<'a>, ~reverse: bool=?, 'a => 'b) => array<'b>
Normal usage would be like before since the flag would be false by default, and the reverse version would be used like this:
let foo = bar->Belt.Array.map(x => x + 1, ~reverse=true)
What would you prefer? If you have other ideas, don’t hesitate to comment!
add a reverse flag to existing functions
add extra functions (mapReverse, forEachReverse, etc)
Something to think about with a reverse flag is that it would add a slight runtime to each function (regardless of whether or not the user enables reverse). It may be so small that it doesn’t matter in practice, but functions like Belt.Array.map are also used quite a lot. A slight performance impact may add up.
Yes for performance reason, initially I had to write this for one of my projects where this was in a hot path, so I thought I could just add it to Belt. I also added joinWith. My idea is to make Belt as feature complete as possible so Js module is not needed anymore.
@namenu The same could be said about e.g.Belt.Array.keepMap, but I still prefer to use that over 2 separate function calls.
Why should I iterate over an array multiple times, when I can achieve the same result with only one iteration?
I haven’t had the usecase for reverse map yet myself. But I think supporting this usecase is a good thing: either by an additional argument or a new function.