A use case for an ordered hash

On 8/21/06, Rick DeNatale [email protected] wrote:

Which, as has been shown can be provided without changes to the base
specification/implementation of Ruby.

Such implementations are a bit slower and less efficient since
they’re pure Ruby.

The fly in the ointment, is that a few of the proponents of
OrderedHash seem to strongly require a literal syntax for the beast.
That’s the one thing which seems to require divine intervention .

Not require, but desire.

-austin

[email protected] wrote:

On Tue, 22 Aug 2006 [email protected] wrote:

A literal syntax would be a bonus but of course would require a change
to core.

not if

require ‘orderedhash’

replaces the builtin Hash

No, I think it would still require a core change – because a
literal would get converted internally before we wanted it to,
right?

Hal

On 8/21/06, [email protected] [email protected] wrote:

replaces the builtin Hash

Which, IIRC, no one but opponents to an (insertion) ordered
associative array with hash-like lookup has proposed seriously.

:wink:

-austin

Phrogz wrote:

hash = [ foo–bar, jim…jam ]

…but now I’m hijacking the thread.

We already do; it’s just that the pompous popinjays refuse to use it.

{ :gee, ‘whiz’, :gosh, ‘darn’ }
==>{:gee=>“whiz”, :gosh=>“darn”}

Coming to Ruby from JavaScript (among other paths) I personally dislike
Ruby’s two-character Hash delimiter. It’s been brought up before, and I
know that the colon makes it harder to parse with symbols, but I wish
we had a one-char (preferably without shift on an English keyboard
layout) hash delimiter.

hash = [ foo:bar, whee~la, dunno|what, delimiter=wouldbe ]

Heck, even two no-shift chars would be a nice improvement:

hash = [ foo–bar, jim…jam ]

…but now I’m hijacking the thread.

On Aug 21, 2006, at 6:25 PM, Phrogz wrote:

Coming to Ruby from JavaScript (among other paths) I personally
dislike
Ruby’s two-character Hash delimiter. It’s been brought up before,
and I
know that the colon makes it harder to parse with symbols, but I wish
we had a one-char (preferably without shift on an English keyboard
layout) hash delimiter.

hash = [ foo:bar, whee~la, dunno|what, delimiter=wouldbe ]

Ask and you shall receive:

$ ruby_yarv -ve ‘p({foo: “bar”, baz: “bar”})’
ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08) [i686-darwin8.7.1]
YARVCore 0.4.1 Rev: 527 (2006-07-19) [opts: [direct threaded code]
[inline method cache] ]
{:foo=>“bar”, :baz=>“bar”}

James Edward G. II

William J. wrote:

We already do; it’s just that the pompous popinjays refuse to use it.

{ :gee, ‘whiz’, :gosh, ‘darn’ }
==>{:gee=>“whiz”, :gosh=>“darn”}

Is that the way the syntax goes?

Hal

Phrogz wrote:

hash = [ foo–bar, jim…jam ]

…but now I’m hijacking the thread.

Hijack away, it’s been discussed to death for years.

I agree I’d prefer a “nicer” notation than we have, but the
one we have is still better than nothing.

I’d like the single colon personally; or even the double colon,
but that already has a meaning.

As for symbols, I’d just require a space (which seems reasonable
to me):

hash = {123 : :abc, 234 : :def}

or even

hash = {123: :abc, 234: :def}

It looks a little funny, but at least it doesn’t have commas
intruding between the key and value.

Hal

On 8/21/06, William J. [email protected] wrote:

We already do; it’s just that the pompous popinjays refuse to use it.

Is it refusal, or a desire to do what is clearest and most compatible?

I’d personally love to see the 1.9 foo: bar format come to 1.8, but
wishes aren’t horses.

-austin

[email protected] wrote:

my alib has an orderedhash which maintains insertion order.

fyi.

He he. Facets has one too called Dictionary (but also aliased as
OrderedHash) But I think Ruby would be better off for have the real
deal written in C.

T.

On Tue, 22 Aug 2006, Trans wrote:

[email protected] wrote:

my alib has an orderedhash which maintains insertion order.

fyi.

He he. Facets has one too called Dictionary (but also aliased as
OrderedHash) But I think Ruby would be better off for have the real
deal written in C.

indeed.

-a

James Edward G. II wrote:

$ ruby_yarv -ve ‘p({foo: “bar”, baz: “bar”})’
ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08) [i686-darwin8.7.1]
YARVCore 0.4.1 Rev: 527 (2006-07-19) [opts: [direct threaded code]
[inline method cache] ]
{:foo=>“bar”, :baz=>“bar”}

Yeehaw! Thanks, powers-that-be! :slight_smile:

Whoa! And auto-symbolizing keys, too? Very, very exciting.

Phrogz wrote:

Is that 1.9? I get:

Bah…don’t mind me. I’m an idiot. Of course that works in 1.8.4 if you
use parens around the hash instead of letting it think it’s a block.

Phrogz wrote:

Whoa! And auto-symbolizing keys, too? Very, very exciting.

It’s pretty cool. The only catch is that your keys have
to be symbols for this coolness to work, right?

Hal

William J. wrote:

{ :gee, ‘whiz’, :gosh, ‘darn’ }
==>{:gee=>“whiz”, :gosh=>“darn”}

Is that 1.9? I get:

C:>ruby -ve “p { :gee, ‘whiz’, :gosh, ‘darn’ }”
ruby 1.8.4 (2006-04-14) [i386-mswin32]
-e:1: syntax error
p { :gee, ‘whiz’, :gosh, ‘darn’ }
^
-e:1: syntax error
p { :gee, ‘whiz’, :gosh, ‘darn’ }
^

On Aug 22, 2006, at 7:16 PM, Hal F. wrote:

It’s pretty cool. The only catch is that your keys have
to be symbols for this coolness to work, right?

Correct. It’s just and alternate symbol Hash key syntax.

James Edward G. II

On 8/21/06, Rick DeNatale [email protected] wrote:

On 8/21/06, Chad P. [email protected] wrote:

On Tue, Aug 22, 2006 at 05:32:52AM +0900, Robert D. wrote:

OrderableHash (maybe better naming)

OrderHash sounds good to me.

Well when I OrderHash, I usually order CornedBeefHash with a
fried egg on top

De gustibus non dispudandum esse .
But sounds nice to me :wink:
What exectly is a hash (cuisine not informatics)?

Robert

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On 8/22/06, Phrogz [email protected] wrote:

Phrogz wrote:

Is that 1.9? I get:

Bah…don’t mind me. I’m an idiot.

Why am I suddenly not feeling lonsome anymore ?

Of course that works in 1.8.4 if you

use parens around the hash instead of letting it think it’s a block.


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

Hello to all,

Phrogz wrote:

$ ruby_yarv -ve ‘p({foo: “bar”, baz: “bar”})’
{:foo=>“bar”, :baz=>“bar”}
Yeehaw! Thanks, powers-that-be! :slight_smile:
Whoa! And auto-symbolizing keys, too? Very, very exciting.

Yes, I think this is useful
and I fully suport it.

And I am 100% in favour of a order-preserving hash in the Ruby core.

And actually, I would prefer the behavior of Hash to be replaced
by the behavior of “HistoryHash”.
Enough people do want this feature,
a history-preserving hash does not cost much more (in terms of memory
and execution time)
and it is consistent with tho old hash behavior.
So, for the sake of simplicity, no new class, let’s just replace
Hash by a “better” Hash.

This brings me to a point that I is very important to me:

      Ruby crystallization.

I believe it is worth not only to add new useful syntax or features,
but also to strip less useful syntax/features.
Perhaps my plea comes a little bit late, now that Ruby already is
among the Tiobe top 20, but I think it is still worth some effort
to bring out the Ruby crystal more clearly.

[For a horror example, think of
the semantics of the switch statement in C.

  • Historically (from the assembler point of view) it was logical
    to have the option of omitting the break statement after a case,
    but later it became a pain for generations of programmers.]

I am glad that some efforts towards “crystallization”
have already been happening:
For example, the use of parallel assignment is permitted in more places.

So as an example of what I think of in terms of syntax unification,
consider this:
Arrays are already very much integrated in the core syntax of Ruby,
but what about Hashes?
Given
def foo(a, b, c)
it is possible to call
array=[1,2,3]; foo(*array) # of course…
but why not
hash={:a=>1,:b=>2,:c=>3}; foo(**hash) # take any new symbol instead of
**

I mean, can we have a Hash-splat operator?
Extend the notion of parallel assignment in
such a way that it not only operates with position
but with name?
Then it would be much more straightforward to use
named paramters (as in Rails).

Is this useful or crazy?
name = “Paul”
city = “Paris”
**hash = name, city
=> {:name=“Paul”,:city=“Paris”}

Of course, questions arise how to deal with unnamed values, …
But my point is “Closer integration of Hashes in the language”.

(And actually, order-preserving hashes would be a more suitable to do
that.)

OK, I got a bit carried away by this example,
the bottom line I was trying to make is:
just as already: every paramerter-list can be an array
let every paramter-list be useable like a hash.

And furthermore: simplification of proc/lambda/method/block.

Of course while/repeat simplification.

So much for now

Sven

Sven S. schrieb:

And I am 100% in favour of a order-preserving hash in the Ruby core.

And actually, I would prefer the behavior of Hash to be replaced
by the behavior of “HistoryHash”.
Enough people do want this feature,
a history-preserving hash does not cost much more (in terms of memory
and execution time)
and it is consistent with tho old hash behavior.
So, for the sake of simplicity, no new class, let’s just replace
Hash by a “better” Hash.

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[:one] = 1
hh[:two] = 2
hh[:one] = 3
hh.each do |k, v| p k end

Should it be

:one
:two

or

:two
:one

Why should it be the one or the other?

Regards,
Pit