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
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
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
I like it, but not the name. partition_by, perhaps.
martin
Yes, this is *much* better!
robert
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
That *is* nice - wonder if there'd be any issues with moving it into
Enumerable.
martin
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
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
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
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
At Tue, 14 Jun 2005 18:44:00 +0900,
mark wrote in [ruby-talk:145336]:
> Or, failing that, how to talk to a gps receiver? I'll need to
> communicate with muxes as well.
I often use gpstrans on Linux.
<http://sourceforge.net/projects/gpstrans/>
--
Nobu Nakada
Ham radio geeks have been at this for a while:
http://users.crosspaths.net/~wallio/HABLinks.html
Also check out APRS: http://web.usna.navy.mil/~bruninga/aprs.html
Steve (N6UNI)
I'll have a look at the automation stuff. I need to be able to read gps
data, and send it back out over a TNC to a 2meter ham radio, so I can
find the darn thing again. I also need to be able to record still
pictures from a digital camera and record a movie from a webcam... all
without resorting to xwindows if possible... as I plan to run under
linux booting into console mode... with no monitor.
Thanks for the kind words!
Mark
Oh ya, the start of a website is at http://www.retrobbs.org/balloon
-----Original Message-----
From: Hal Fulton [mailto:hal...@hypermetrics.com]
Sent: 15 June 2005 00:19
To: ruby-talk ML
Subject: Re: Crazy Balloon flight computer software...
If anyone has any info on this in the UK (I know this is getting off
topic) I'd sure appreciate it.
Thanks for the links!
Mark
Check this out: www.gumstix.org
I found that gumstix board a while back and decided to dive right in to the
realm of microcontrollers. Never tried compiling ruby on it, don't know how
easy that would be, maybe others have done it.
-Jeff
----- Original Message -----
From: "Hal Fulton" <hal...@hypermetrics.com>
To: "ruby-talk ML" <ruby...@ruby-lang.org>
Sent: Tuesday, June 14, 2005 5:18 PM
Subject: Re: Crazy Balloon flight computer software...