Closed JoshuaHall closed 2 years ago
Hey @JoshuaHall ,
That is pretty cool!
What lead to you making this function? Did you run into a use case where you needed high performance reverse range? What was it, if you dont mind me asking?
Hi, when I was forking elm-chronographify (for educational purposes, I was originally learning about how to make a timer/stopwatch in Elm) I decided that it would make the code cleaner to implement this function. Otherwise I had to do a List.reverse
, then a List.indexedMap
, then finally a List.reverse
again. With reverseRange
, I could then implement my own reversedIndexedMap
, except within it I operated over the reverseRange
of the List.length list
to 1
, not (List.length list) - 1
to 0
(this removed the need to increment the index in the view, which again made the code slightly cleaner in my opinion).
The performance hasn't really come into play for elm-chronogrpahify
, but I think for other apps that need something like this it could be very useful. However, the biggest benefit in my opinion is still the slightly shorter and clearer code.
@prikhi I agree. I initially just copied elm/core
's List.range
implementation and changed a few bits, but that seems nicer. I just committed it.
I am still thinking about this. Right now I am leaning towards including this new function in List.Extra
.
Usually I like better use case examples. Just having a shorter and simpler function in itself isn't all that compelling of a reason to include this. It doesnt encapsulate very much complexity that developers would struggle to implement on their own.
But it is optimized, and it's nice that List.Extra
includes a lot of optimized versions of basic functions.
Alright, I like it! Lets merge it. Thanks @JoshuaHall
Added reverseRange, a faster way of creating a reversed range.
Benchmarks (using elm-explorations/benchmark) on my machine show it is roughly 50% faster at most list sizes.
I also added a fuzz test for reverseRange. The range ceiling is there to prevent a heap error I was getting when using the
int
fuzzer.