# Array::uniq { block }?

**URL:** <https://rubytalk.org/t/array-uniq-block/16048>\
**Category:** ruby-talk\
**Created:** [26 January 2005 15:33 UTC](https://rubytalk.org/t/array-uniq-block/16048 "2005-01-26T15:33:54Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [30 January 2005 14:30 UTC](https://rubytalk.org/t/array-uniq-block/16048/21 "2005-01-30T14:30:48Z")

</div>

"Pit Capitain" \<pit@capitain.de\> schrieb im Newsbeitrag news:41FCBFE5.4000405@capitain.de...

> Trans schrieb:
> 
> > Has anyone, could anyone, write a Ruby version of this method? I'm not  
> > sure what its supposed to do exaclty. Id like to see source.
> 
> No problem:
> 
> &nbsp;&nbsp;require 'set'
> 
> &nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;def uniq\_by  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = Set.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |elem|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield elem  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless values.include? value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values \<\< value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< elem  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;p ( 0 .. 9 ).to\_a.uniq\_by { |x| x % 4 } # =\> [0, 1, 2, 3]
> 
> I bet Robert will transform this into a version using inject 😉

Since you asked... :-))

module Enumerable  
&nbsp;&nbsp;def uniq\_by  
&nbsp;&nbsp;&nbsp;&nbsp;inject({}) {|h,x| h[yield(x)] ||= x; h}.values  
&nbsp;&nbsp;end  
end

> > a=(1..10).map {rand 10}

=\> [2, 7, 4, 1, 9, 0, 0, 2, 5, 0]

> > a.uniq\_by {|x| x%3}

=\> [9, 7, 2]

Pro:  
- shorter  
- needs one less temp instace

Con:  
- not stable, original order is not maintained

A stable version:

module Enumerable  
&nbsp;&nbsp;def uniq\_by  
&nbsp;&nbsp;&nbsp;&nbsp;h = {}; inject() {|a,x| h[yield(x)] ||= a \<\< x}  
&nbsp;&nbsp;end  
end

> > a.uniq\_by {|x| x%3}

=\> [2, 7, 9]

Beautiful about this version is that it does not need the "; h" at the end of the inject block and no ".values", too... 🙂

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [30 January 2005 15:05 UTC](https://rubytalk.org/t/array-uniq-block/16048/22 "2005-01-30T15:05:53Z")

</div>

"Florian Gross" \<flgr@ccan.de\> schrieb im Newsbeitrag news:36482aF4tni5tU1@individual.net...

> Pit Capitain wrote:
> 
> > Trans schrieb:
> > 
> > > Has anyone, could anyone, write a Ruby version of this method? I'm not  
> > > sure what its supposed to do exaclty. Id like to see source.
> > 
> > No problem:
> > 
> > &nbsp;&nbsp;require 'set'
> > 
> > &nbsp;&nbsp;class Array  
> > &nbsp;&nbsp;&nbsp;&nbsp;def uniq\_by  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result =   
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = Set.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |elem|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield elem  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless values.include? value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values \<\< value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< elem  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;p ( 0 .. 9 ).to\_a.uniq\_by { |x| x % 4 } # =\> [0, 1, 2, 3]
> > 
> > I bet Robert will transform this into a version using inject 😉
> 
> module Enumerable  
> &nbsp;&nbsp;def uniq\_by()  
> &nbsp;&nbsp;&nbsp;&nbsp;inject() do |state, item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state.include?(value) ? state : state + [item]  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

Hmmm... When deciding whether to include an element or not you mix values and converted values. That's not the same as what the implementation above does.

Also (as optimization) you could use "state \<\< item" instead of "state + [item]" because Array#\<\< returns self. This saves you a lot temporary 1element arrays and avoids creating new arrays all the time (Array#+ creates a new Array). 🙂

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 January 2005 21:25 UTC](https://rubytalk.org/t/array-uniq-block/16048/23 "2005-01-30T21:25:48Z")

</div>

Thanks, now an offical part of Facets (as of next release).

T

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [31 January 2005 12:55 UTC](https://rubytalk.org/t/array-uniq-block/16048/24 "2005-01-31T12:55:53Z")

</div>

Very nice indeed.

martin

> **···**
>
> Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > module Enumerable  
> > &nbsp;&nbsp;def uniq\_by  
> > &nbsp;&nbsp;&nbsp;&nbsp;h = {}; inject() {|a,x| h[yield(x)] ||= a \<\< x}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > \>\> a.uniq\_by {|x| x%3}  
> > =\> [2, 7, 9]
> > 
> > Beautiful about this version is that it does not need the "; h" at the end  
> > of the inject block and no ".values", too... 🙂

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [31 January 2005 18:40 UTC](https://rubytalk.org/t/array-uniq-block/16048/25 "2005-01-31T18:40:46Z")

</div>

Robert Klemme wrote:

> Also (as optimization) you could use "state \<\< item" instead of "state + [item]" because Array#\<\< returns self. This saves you a lot temporary 1element arrays and avoids creating new arrays all the time (Array#+ creates a new Array). 🙂

I dislike having the block of .inject having side effects. That sort of defeats the purpose IMHO.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [31 January 2005 19:34 UTC](https://rubytalk.org/t/array-uniq-block/16048/26 "2005-01-31T19:34:59Z")

</div>

Hi --

> Robert Klemme wrote:
> 
> > Also (as optimization) you could use "state \<\< item" instead of "state + [item]" because Array#\<\< returns self. This saves you a lot temporary 1element arrays and avoids creating new arrays all the time (Array#+ creates a new Array). 🙂
> 
> I dislike having the block of .inject having side effects. That sort of defeats the purpose IMHO.

I don't think it's a side effect, since it's just injecting something  
into the accumulator or "injectee". This might be exactly what you  
want if, for example, you have multiple references to the accumulator:

&nbsp;&nbsp;&nbsp;x =   
&nbsp;&nbsp;&nbsp;y = x

&nbsp;&nbsp;&nbsp;["a","b","c"].inject(x) {|m,n| m \<\< n.upcase}

&nbsp;&nbsp;&nbsp;puts y # ["A", "B", "C"]

Otherwise in a case like this you could just use #map.

David

> **···**
>
> On Tue, 1 Feb 2005, Florian Gross wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

[Previous page](https://rubytalk.org/t/array-uniq-block/16048.md?page=1)
