A use case for an ordered hash

H

Hal Fulton

Pit said:
Sven, regarding the "HistoryHash" part of your post:

In order to replace the standard Hash with a "better" Hash, there should
be a clear agreement on its desired behaviour. I don't think this
agreement has been achieved yet. What should be the result of the
following code?

hh = HistoryHash.new
hh[:eek:ne] = 1
hh[:two] = 2
hh[:eek:ne] = 3
hh.each do |k, v| p k end

Should it be

:eek:ne
:two

or

:two
:eek:ne

Why should it be the one or the other?

An excellent point, and there are other similar questions
to be answered.

As for replacing Hash with another implementation, I would
be OK with it (if it proved reasonably speedy).

Others would not want it even if it were the same speed
as before.

I think the people calling for an ordered hash are a sizable
group, but still a minority.


Hal
 
H

Hal Fulton

The pragmatic approach (and what I imagine will happen) is that if
someone wants these, they should write them. If they're then widely
used, they should go in to the standard distribution.

Yes. The only catch is that a full implementation (with literals)
requires a change in the Ruby interpreter.


Hal
 
S

surf

Hal said:
There have been numerous occasions when I wanted an
ordered hash, but usually I can't remember to write
them down.

Here's just one.

Once I wanted a "dynamic case statement" of sorts.
I wanted to use procs as values in a hash. Something
like:

actions = { /abcd/ => lambda { do_this },
/xyz/ => lambda { do_that },
/abc/ => lambda { other }}

Then I could just iterate through the keys looking
for a match, then use that key to find the associated
proc and call it.

However, I quickly noticed that this is no good. The
order of iteration is unpredictable. So I couldn't
guarantee that (for example) /abcd/ would be tested
before /abc/, and so on.

So yeah, I ended up using an array of arrays. But it
just felt wrong.


Hal

Can't you create your own using mixins and including enumerable or
something like that ?
 
H

Hal Fulton

surf said:
Can't you create your own using mixins and including enumerable or
something like that ?

Yes, but I would lose the convenient literal notation.

Hal
 
A

Austin Ziegler

Yes, but I would lose the convenient literal notation.

...and which could have been seen by someone who looked at more than
the last two messages of a thread.

-austin
 
H

Hal Fulton

Austin said:
...and which could have been seen by someone who looked at more than
the last two messages of a thread.

Heh heh. That's OK. We were at "that point" in the fugue.

Hal
 
S

surf

Hal said:
Yes, but I would lose the convenient literal notation.

Hal


I was curious about this, so I had to try it:

================================================

class OrderedHash

include Enumerable


def initialize(*parm)
@arr = parm
@hash = {}
parm.each do |p|
p.each do |k,v|
@hash[k] = v
end
end
end

def []=(idx, val)
el = {idx => val}
@arr << el
@hash[idx] = val
end

def [](idx)
@hash[idx]
end

def each
@arr.each do |e|
e.each do |k,v|
yield k,v
end
end
end

end


# need each paramter in {}, could also work out a way to do
# [:frog,"green", :sky, "blue" etc ]

thingColor = OrderedHash.new({:frog => "green"},
{:sky => "blue"},
{:elephant => "pink"},
{:scarf => "red"}

)

puts "the sky is #{thingColor[:sky]}\n\n"

thingColor[:limo] = "black"
thingColor[:snowman] = "white"


thingColor.each do |k,v|
puts "#{k} = #{v}"
end
 
A

Austin Ziegler

I was curious about this, so I had to try it:

Note: There are about a dozen implementations of an insertion ordered
hash-like objects. I've written one. Hal has probably written one. Ara
Howard has written one, I think.

All that's being discussed is whether it's worthwhile making a C
version that has a *new* literal syntax to create insertion ordered
hash-like objects. The main concerns are (1) what literal syntax, and
(2) what name. Since we're talking something *new*, we're not talking
something that would affect the performance for people who don't need
the functionality.

Just saying that we can implement this in Ruby -- or proviing such an
implementation -- isn't helpful in the least. We know this. We've done
this. We're looking for something different.

-austin
 
S

surf

Austin said:
Note: There are about a dozen implementations of an insertion ordered
hash-like objects. I've written one. Hal has probably written one. Ara
Howard has written one, I think.

All that's being discussed is whether it's worthwhile making a C
version that has a *new* literal syntax to create insertion ordered
hash-like objects. The main concerns are (1) what literal syntax, and
(2) what name. Since we're talking something *new*, we're not talking
something that would affect the performance for people who don't need
the functionality.

Just saying that we can implement this in Ruby -- or proviing such an
implementation -- isn't helpful in the least. We know this. We've done
this. We're looking for something different.

I started to get the idea to such an effect, or that some short hand
notation was desired although on my last post Hal just said it wouldn't
work due to literal notation.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top