[Q] removing array duplicates where a subset is unique

D

David A. Black

Hi --

I do to! It looks like a very angry gnome.

I'm still waiting to see a use-case for &&=. I've come close to
thinking I had one once or twice, but it always turns out I didn't.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 
C

Chuck Remes

That's well and good, but in the process of using a hash to remove the
duplicates, the result will be out of order. See example below.

True, but in my case the order only matters for *picking* the first of
the duplicates. I understand hashes in 1.8 don't maintain order, but
that isn't relevant since I don't care about output order.

I suppose I could have written a longer explanation of what I needed,
but I favored brevity over those other details.

cr
 
C

Chuck Remes

David said:
Hi --

[2, 2, 3, 40]
h[key] = suba
end

end

p h.values

--output:--
[[2, 2, 3, 40], [1, 2, 3, 10]]

I'm not sure what that buys you, though. The ||= idiom should work
fine, unless you need to do something extra during the if statement.


David

Facets has enumerable#uniq_by implemented like this:

def uniq_by #:yield:
h = {}; inject([]) {|a,x| h[yield(x)] ||= a << x}
end

Ooh, I like this one too. I'll try it out and see what kind of
performance I get. I understand that #inject performance is weak in
MRI 1.8, but I primarily use JRuby where it doesn't share the same
performance issues as the C version.

cr
 
J

Joel VanderWerf

David said:
I'm still waiting to see a use-case for &&=. I've come close to
thinking I had one once or twice, but it always turns out I didn't.

Not terribly useful, but...

line = sock.gets
line &&= line.chomp
log.debug "line is #{line.inspect}"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top