[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

finding Hash subsets based on key value

ee

6/12/2005 10:56:00 PM

Hi

Is there a simple way of searching the keys of a Hash to find a subset.
I have a set of keys with two values indicating group and sub-group,
eg:

group_1_item_1
group_2_item_2
group_1_item_2
group_1_item_4
group_2_item_3

I'd like to divide them into groups, but without having to loop through
the entire collection and having to test each key

Regards
Erik

16 Answers

Martin DeMello

6/13/2005 4:19:00 AM

0

ee <erik.eide@gmail.com> wrote:
> Hi
>
> Is there a simple way of searching the keys of a Hash to find a subset.
> I have a set of keys with two values indicating group and sub-group,
> eg:
>
> group_1_item_1
> group_2_item_2
> group_1_item_2
> group_1_item_4
> group_2_item_3
>
> I'd like to divide them into groups, but without having to loop through
> the entire collection and having to test each key

You can't get away without testing each key under the hood (this is
intrinsically an O(n) problem), but this should make it syntactically
convenient at least:

hash.partition {|k,v| k =~ /group_1/ }.map {|i| Hash[*i.flatten]}

Enumerable#partition divides your collection into two parts, based on
whether the block returns true or false, so it doesn't scale to more
than two subsets, but for your example it should work nicely.

martin

Robert Klemme

6/13/2005 7:38:00 AM

0

Martin DeMello wrote:
> ee <erik.eide@gmail.com> wrote:
>> Hi
>>
>> Is there a simple way of searching the keys of a Hash to find a
>> subset. I have a set of keys with two values indicating group and
>> sub-group, eg:
>>
>> group_1_item_1
>> group_2_item_2
>> group_1_item_2
>> group_1_item_4
>> group_2_item_3
>>
>> I'd like to divide them into groups, but without having to loop
>> through the entire collection and having to test each key
>
> You can't get away without testing each key under the hood (this is
> intrinsically an O(n) problem),

Definitely!

> but this should make it syntactically
> convenient at least:
>
> hash.partition {|k,v| k =~ /group_1/ }.map {|i| Hash[*i.flatten]}
>
> Enumerable#partition divides your collection into two parts, based on
> whether the block returns true or false, so it doesn't scale to more
> than two subsets, but for your example it should work nicely.

IMHO we should have a general partitioning method (probably in Enumerable)
that does partitioning into several buckets and not just two. Something
like this maybe:

module Enumerable
def general_partition
parts = Hash.new {|h,k| h[k] = self.class.new}
each do |x|
parts[yield(x)] << x
end
parts
end
end

class Hash
def << (x) self[x[0]] = x[1] end
end

>> h={1=>1,2=>2,3=>3}
=> {1=>1, 2=>2, 3=>3}
>> h.general_partition {|k,v| k % 3}
=> {0=>{3=>3}, 1=>{1=>1}, 2=>{2=>2}}

Opinions?

Kind regards

robert

Martin DeMello

6/13/2005 8:41:00 AM

0

Robert Klemme <bob.news@gmx.net> wrote:
>
> IMHO we should have a general partitioning method (probably in Enumerable)
> that does partitioning into several buckets and not just two. Something
> like this maybe:
>
> module Enumerable
> def general_partition

I like it, but not the name. partition_by, perhaps.

martin

Robert Klemme

6/13/2005 8:52:00 AM

0

Martin DeMello wrote:
> Robert Klemme <bob.news@gmx.net> wrote:
>>
>> IMHO we should have a general partitioning method (probably in
>> Enumerable) that does partitioning into several buckets and not just
>> two. Something like this maybe:
>>
>> module Enumerable
>> def general_partition
>
> I like it, but not the name. partition_by, perhaps.

Yes, this is *much* better!

robert

James Gray

6/13/2005 1:10:00 PM

0

On Jun 13, 2005, at 3:55 AM, Robert Klemme wrote:

> Martin DeMello wrote:
>
>> Robert Klemme <bob.news@gmx.net> wrote:
>>
>>>
>>> IMHO we should have a general partitioning method (probably in
>>> Enumerable) that does partitioning into several buckets and not just
>>> two. Something like this maybe:
>>>
>>> module Enumerable
>>> def general_partition
>>>
>>
>> I like it, but not the name. partition_by, perhaps.
>>
>
> Yes, this is *much* better!

The Set class has this feature and I even like the name:

irb(main):001:0> require "set"
=> true
irb(main):002:0> langs = Set.new([:perl, :python, :ruby])
=> #<Set: {:perl, :python, :ruby}>
irb(main):003:0> langs.classify { |m| m.to_s[0, 1] }
=> {"p"=>#<Set: {:perl, :python}>, "r"=>#<Set: {:ruby}>}

James Edward Gray II



Martin DeMello

6/14/2005 9:33:00 AM

0

James Edward Gray II <james@grayproductions.net> wrote:
>
> The Set class has this feature and I even like the name:
>
> irb(main):001:0> require "set"
> => true
> irb(main):002:0> langs = Set.new([:perl, :python, :ruby])
> => #<Set: {:perl, :python, :ruby}>
> irb(main):003:0> langs.classify { |m| m.to_s[0, 1] }
> => {"p"=>#<Set: {:perl, :python}>, "r"=>#<Set: {:ruby}>}

That *is* nice - wonder if there'd be any issues with moving it into
Enumerable.

martin

Mark Firestone

6/14/2005 9:44:00 AM

0

....gonna launch a computerized weather balloon, of couse!

And of course, I need to write the flight control software in Ruby,
because it is my favourite language. Anyone know how to control a
digital camera from ruby (under linux...)

Or, failing that, how to talk to a gps receiver? I'll need to
communicate with muxes as well.

Thanks,

Mark

http://www.retrobbs.o...



Pit Capitain

6/14/2005 9:49:00 AM

0

Martin DeMello schrieb:
>>irb(main):002:0> langs = Set.new([:perl, :python, :ruby])
>>=> #<Set: {:perl, :python, :ruby}>
>>irb(main):003:0> langs.classify { |m| m.to_s[0, 1] }
>>=> {"p"=>#<Set: {:perl, :python}>, "r"=>#<Set: {:ruby}>}
>
> That *is* nice - wonder if there'd be any issues with moving it into
> Enumerable.

Note that Set#classify creates a set for each group, which might not
always be what you want. IMO, Enumerable#classify should create arrays
instead.

Regards,
Pit


Martin DeMello

6/14/2005 9:59:00 AM

0

Pit Capitain <pit@capitain.de> wrote:
>
> Note that Set#classify creates a set for each group, which might not
> always be what you want. IMO, Enumerable#classify should create arrays
> instead.

I like Robert Klemme's implementation, where each Enumerable creates
partitions of its own type, and calls << on them to populate them. OTOH
it adds an additional dependency to Enumerable (<< as well as each), but
maybe it could default to Array if << doesn't exist.

martin

Hal E. Fulton

6/14/2005 11:19:00 PM

0

mark wrote:
> ....gonna launch a computerized weather balloon, of couse!
>
> And of course, I need to write the flight control software in Ruby,
> because it is my favourite language. Anyone know how to control a
> digital camera from ruby (under linux...)
>
> Or, failing that, how to talk to a gps receiver? I'll need to
> communicate with muxes as well.

I can't help you offhand, but I just wanted to say this is the
coolest thing I've heard of in a month or more.

Actually, as for controlling devices... you might look into the
home automation realm. There are some Linux-based solutions there,
probably including security cameras and such.


Hal