-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add sort methods to Indexable::Mutable
#11195
Add sort methods to Indexable::Mutable
#11195
Conversation
Returning covariant containers from module Indexable::Mutable(T)
def reverse
x = dup
x.reverse!
end
def rotate
x = dup
x.rotate!
end
def shuffle(random)
x = dup
x.shuffle!(random)
end
def map(& : T -> T) # same return type only
x = dup
x.map! { |elem| yield elem }
end
end I think this warrants more discussion. For now only the actual mutating methods should be added. |
What does "covariant" mean in this context? That we can't assume which type the implementation of But I agreed that these methods should probably not bei merged like that. |
@HertzDevil Those examples may work, but those are pretty inefficient as implementations go as it would force all contents to be streamed through memory twice. Which may be noticable for big containers. That may be ok for O(nlog) operations like sort, but should probably be avoided for operations that are O(n) in any case. |
@HertzDevil care to expand a bit? I don't think I get your comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. In the future I think it's best to add the dup method somewhere in the hierarchy, although I'm not sure where it should be.
Closing. The main parts were merged in #11254 |
This patch moves the sort methods from
Slice
andArray
intoIndexable::Mutable
as suggested in #11057 (comment). It is an alternative to #11057.All mutable indexables support mutating sort, although the default implementation is not very efficient because it duplicates the collection to a slice for sorting.
Array
(andSlice
obviously) overrides the mutating sort methods to provide a more efficient implementation by directly operating on the buffer. A similar approach can be used for other data types that represent the values in such a way (e.g.StaticArray
: #10889).Non-mutating sort methods are certainly not limited to mutable collections, but mutable collections offer a simple implementation, based on
#dup
and the mutating sort methods.#dup
is not required to be implemented on inheriting types, but it should be pretty common (allIndexable::Mutable
types in stdlib implement#dup
except). So this is a kind of weak dependency, but IMO it's acceptable.StaticArray
We could consider adding
#dup
as an abstract def toIndexable::Mutable
to make it explicit. But I'm not sure that's really necessary.Closes #11057