-
Notifications
You must be signed in to change notification settings - Fork 101
Add Array#to_ranges #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Array#to_ranges #265
Conversation
I needed this code for a project and figured I'd offer it up to facets I originally found this code at https://dzone.com/articles/convert-ruby-array-ranges but it didn't actually work as described, so I fixed it.
Looks good to me. But the tests didn't run for some reason. I don't think it's your code. I'll look into it and get back to you. |
It's odd that travis blew up. Can we make Compact is O(N). Uniq might be O(N^2) search? sort is okay. However, it's also making 3 copies of the array for no reason. Could be even self.dup.sort!.uniq!.compact! or something like that. |
|
Yes, but it should be a new array.
|
Well, it depends on the assumptions you want to make, but I prefer not doing additional allocations.
As an example, sort returns a new array, so it supports your argument. |
In fact something like: def to_ranges
ranges = []
return ranges if empty?
array = uniq.compact
return ranges if array.empty?
array.sort!
... And get rid of the |
While we are on the topic of bike shedding lol, perhaps |
Anyway if it can be improved with the above advice I think it's a good addition. |
In this case you really have to because if you return
And that's not what would be expected. |
I suspect that the algorithm won't do what one expects then. I have to try it, but I expect we'd get things like,
And I don't think that would ever be what anyone wants. That being so the @aguynamedryan What do you think? |
Hey I just rediscovered that Facets already has this, arrange. So the task now becomes to compare implementations, and improve the code accordingly. Also, I think we need to ask if the name "arrange" is really the right one. Perhaps at the very least, we could alias |
Well, there is a |
The name |
@ioquatix You know, you are right. I'll change it to Someone remind me to do this if I haven't gotten around to it within the next few weeks. |
I needed this code for a project and figured I'd offer it up to facets
I originally found this code at
https://dzone.com/articles/convert-ruby-array-ranges
but it didn't actually work as described, so I fixed it.