ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:69836] [Ruby trunk - Feature #11323] [Open] Documentation update on how uniq works / guarantee of order
       [not found] <redmine.issue-11323.20150702013920@ruby-lang.org>
@ 2015-07-02  1:39 ` wilburlo
  2015-07-02  3:34 ` [ruby-core:69837] [Ruby trunk - Feature #11323] " 0x0dea+redmine
  1 sibling, 0 replies; 2+ messages in thread
From: wilburlo @ 2015-07-02  1:39 UTC (permalink / raw)
  To: ruby-core

Issue #11323 has been reported by Daniel Lo.

----------------------------------------
Feature #11323: Documentation update on how uniq works / guarantee of order
https://bugs.ruby-lang.org/issues/11323

* Author: Daniel Lo
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Greetings,

I was looking at Array.uniq and I was wondering how the code made the array unique.

There are 2 different possible outcomes for making an array unique.

For example:
[1,2,1]

The first value is kept and all subsequent duplicate values are removed: [1,2]
or
The array is made unique, order is not retained: [2,1]

Would the ruby team consider adding a guarantee of order (first seen/first kept) is adding this to the Array.uniq specification?  This is what happens in practice (irb), having this as part of the specification would be nice.

I looked at the code http://ruby-doc.org/core-2.2.1/Array.html#method-i-uniq however, I wasn't able to determine exactly how it worked. :(

Thank you!

Regards,

Daniel Lo




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:69837] [Ruby trunk - Feature #11323] Documentation update on how uniq works / guarantee of order
       [not found] <redmine.issue-11323.20150702013920@ruby-lang.org>
  2015-07-02  1:39 ` [ruby-core:69836] [Ruby trunk - Feature #11323] [Open] Documentation update on how uniq works / guarantee of order wilburlo
@ 2015-07-02  3:34 ` 0x0dea+redmine
  1 sibling, 0 replies; 2+ messages in thread
From: 0x0dea+redmine @ 2015-07-02  3:34 UTC (permalink / raw)
  To: ruby-core

Issue #11323 has been updated by D.E. Akers.


`Array#uniq` is implemented using a hash table; since hashes in Ruby preserve insertion order, so too does `Array#uniq`. The following simplified translation to Ruby should help clarify what's going on in `rb_ary_uniq()`:

``` ruby
class Array
  def uniq
    hash = {}
    each { |val| hash[val] = val }
    hash.values
  end
end
```

There is precedent in `Array`'s documentation for mentioning that a method is order-preserving, and it seems that `#uniq` should certainly do so.

----------------------------------------
Feature #11323: Documentation update on how uniq works / guarantee of order
https://bugs.ruby-lang.org/issues/11323#change-53235

* Author: Daniel Lo
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Greetings,

I was looking at Array.uniq and I was wondering how the code made the array unique.

There are 2 different possible outcomes for making an array unique.

For example:
[1,2,1]

The first value is kept and all subsequent duplicate values are removed: [1,2]
or
The array is made unique, order is not retained: [2,1]

Would the ruby team consider adding a guarantee of order (first seen/first kept) is adding this to the Array.uniq specification?  This is what happens in practice (irb), having this as part of the specification would be nice.

I looked at the code http://ruby-doc.org/core-2.2.1/Array.html#method-i-uniq however, I wasn't able to determine exactly how it worked. :(

Thank you!

Regards,

Daniel Lo




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-02  3:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11323.20150702013920@ruby-lang.org>
2015-07-02  1:39 ` [ruby-core:69836] [Ruby trunk - Feature #11323] [Open] Documentation update on how uniq works / guarantee of order wilburlo
2015-07-02  3:34 ` [ruby-core:69837] [Ruby trunk - Feature #11323] " 0x0dea+redmine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).