From: "Shot (Piotr Szotkowski)" Date: 2009-03-30T05:31:19+09:00 Subject: Re: Is there an idiom for mass-populating Hash keys? --ReaqsoxgOBHFXBhH Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Rick DeNatale: > On Sun, Mar 29, 2009 at 12:05 PM, Shot (Piotr Szotkowski) wr= ote: >> class Graph >> def initialize enum >> @vertices =3D {} >> enum.each { |vertex| @vertices[vertex] =3D nil } >> end >> end > Why bother? The only reason I can thing of is that you want to use > @vertices.keys to get the collection of vertices, but why be indirect. > class Graph > def initialize(enum) > @vertices =3D enum > @labels =3D {} > end > end > @labels[anything] will return nil if anything isn't a key. Right, I was thinking about this approach on and off all day today,=20 actually. :) The catch is I=E2=80=99ll be doing various things to this grap= h,=20 like merging vertices together, grouping them by their labels (graph=20 colouring), and doing similar operations on (labeled) edges. While I do like the cleanliness of the approach with @vertices being=20 just a Set, @edges being just a Set of two-element Sets, and there being=20 two additional Hashes for vertex and edge labels, I=E2=80=99m not sure I wa= nt=20 the overhead of making sure there aren=E2=80=99t any leftover keys in the l= abel=20 Hashes after the graph drops some of its edges/vertices =E2=80=93 in other= =20 words, anytime I delete a vertex or an edge I=E2=80=99d have to delete it f= rom=20 two places (either that, or anytime I query the labels I=E2=80=99d have to = make=20 sure I=E2=80=99m filtering out no-longer-existing vertices/edges =E2=80=93 = which is even=20 uglier). Note: I do know about GRATR, but it seems unmaintained; I found=20 (granted, minor) bugs in it by simply requiring it with warnings=20 enabled, and it requires (again, minor) patching to get it working with Ruby 1.9, but my patches to fix both are ignored (and there haven=E2= =80=99t=20 been a relase for a year and a half). I would also use just a tiny=20 fraction of GRATR=E2=80=99s functionality, so it=E2=80=99s simpler for me t= o have my own Graph class (and be able to easily RubyInline parts of it if/when=20 I need the performance). =E2=80=94 Shot --=20 Is your keyboard jammed, or are you just writing Haskell? [Avdi Grimm] --ReaqsoxgOBHFXBhH Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAknP0soACgkQi/mCfdEo8UpvRwCgqZdP/EaCHyS3RLMl3CnGaBoL +yUAoJoY8aPYIFDfuAFuyaqHeCQOGgzH =WJad -----END PGP SIGNATURE----- --ReaqsoxgOBHFXBhH--