elm-community / list-extra

Convenience functions for working with List.
http://package.elm-lang.org/packages/elm-community/list-extra/latest
MIT License
135 stars 58 forks source link

Added reverseRange, a faster way of creating a reversed range #134

Closed JoshuaHall closed 2 years ago

JoshuaHall commented 4 years ago

Added reverseRange, a faster way of creating a reversed range.

reverseRange 20 1 == List.range 1 20 |> List.reverse

Benchmarks (using elm-explorations/benchmark) on my machine show it is roughly 50% faster at most list sizes.

Benchmark comparing the naive implementation vs reverseRange

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.

Chadtech commented 4 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?

JoshuaHall commented 4 years ago

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.

JoshuaHall commented 4 years ago

@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.

Chadtech commented 4 years ago

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.

Chadtech commented 2 years ago

Alright, I like it! Lets merge it. Thanks @JoshuaHall