Hash#update

Has anyone ever proposed making Hash#+ a nondestructive equivalent of
Hash#update ?

I’m experimenting with keyword arguments and find myself passing things
like h1.dup.update(h2), where h1+h2 would feel more natural. h1 is
usually some often-used package of default args, and h2 specifies a few
changes and/or additions.

Mark

Has anyone ever proposed making Hash#+ a nondestructive equivalent of
Hash#update ?

Just my obfuscated memory but…

It was proposed early days and was denied due to a semantic problem,
and that was the reason Hash#update was introduced.

I’m experimenting with keyword arguments and find myself passing things
like h1.dup.update(h2), where h1+h2 would feel more natural. h1 is
usually some often-used package of default args, and h2 specifies a few
changes and/or additions.

That’s exactly the problem: That causes h1+h2 and h2+h1 yield
different hashes. Is this enough reasonable meaning? OK, String#+
has also such meaning, however it is concatenation and both operands
are fully preserved in a result.

Who can define good semantics with that “general” people agree under
the name of `Hash#+'?

While Hash#update is an enough reasonable name, isn’t it?

On the other hand large number of people won’t be confused,
`h1.dup.update(h2)’ is good candidate of meaning of Hash#+.

To vote?

···

In message 3E05E170.30103@iastate.edu ms@iastate.edu writes:


kjana@dm4lab.to December 23, 2002
Art is long, and time is fleeting.

“YANAGAWA Kazuhisa” kjana@dm4lab.to wrote in message
news:20021222164523.DA84B1EE12@milestones.dm4lab.to…

That’s exactly the problem: That causes h1+h2 and h2+h1 yield
different hashes. Is this enough reasonable meaning? OK, String#+
has also such meaning, however it is concatenation and both operands
are fully preserved in a result.

What about using the ‘^’ operator?
In other languages it is also used for string concatenation.

Mikkel

YANAGAWA Kazuhisa wrote:

Has anyone ever proposed making Hash#+ a nondestructive equivalent of
Hash#update ?

Just my obfuscated memory but…

It was proposed early days and was denied due to a semantic problem,
and that was the reason Hash#update was introduced.

I’m experimenting with keyword arguments and find myself passing things
like h1.dup.update(h2), where h1+h2 would feel more natural. h1 is
usually some often-used package of default args, and h2 specifies a few
changes and/or additions.

That’s exactly the problem: That causes h1+h2 and h2+h1 yield
different hashes. Is this enough reasonable meaning? OK, String#+
has also such meaning, however it is concatenation and both operands
are fully preserved in a result.

Who can define good semantics with that “general” people agree under
the name of `Hash#+'?

As you say, non-commutative ‘+’ is common enough in programming.
Array#+ is another example. Consider also that there is an intuitive
temporal sense to these expressions. In (hash1 + hash2), we are
starting with hash1. After hash2 is brought into the picture, the newer
bits would be expected to take precedence. I don’t really see a problem
here.

Regarding both operands being preserved, this is a good point, but in
none of the other ‘+’ methods (numbers, strings, arrays) can the
operands quite be recovered from the result. You get a new object that
is different from the first two; the operands were brought together in a
way that makes sense for the type.

While Hash#update is an enough reasonable name, isn’t it?

True, but Hash#update lives over in Destructive Methods Land. :slight_smile:

On the other hand large number of people won’t be confused,
`h1.dup.update(h2)’ is good candidate of meaning of Hash#+.

That really sums it up. I don’t think Hash#+ can reasonably have a
different meaning.

···

In message 3E05E170.30103@iastate.edu >ms@iastate.edu writes:

To vote?

Hi,

···

In message “Re: Hash#update” on 02/12/23, Mark Slagell ms@iastate.edu writes:

As you say, non-commutative ‘+’ is common enough in programming.
Array#+ is another example. Consider also that there is an intuitive
temporal sense to these expressions. In (hash1 + hash2), we are
starting with hash1. After hash2 is brought into the picture, the newer
bits would be expected to take precedence. I don’t really see a problem
here.

No information lost by String#+ or Array#+, but Hash#update may lose
the value. That’s why I rejected the name “+”. It is possible to
provide the non destructive “update” under different name. Good name?

						matz.

Hello MikkelFJ,

···

On Monday, December 23, 2002, 4:59:00 AM, you wrote:

What about using the ‘^’ operator?
In other languages it is also used for string concatenation.

Mikkel

I think

hash1 | hash2

would be good. It tends to communicate nondestructiveness.

Gavin

Gavin Sinclair wrote:

I think

hash1 | hash2

would be good. It tends to communicate nondestructiveness.

That’s especially good if you’re using hashes as “characteristic
functions” of sets, in which case this ‘|’ is union:

class Hash
def |(h)
dup.update(h)
end
end

elves = {‘Legolas’ => true, ‘Galadriel’ => true}

reindeer = {‘Dasher’ => true, ‘Dancer’ => true,
‘Prancer’ => true, ‘Vixen’ => true,
‘Donner’ => true, ‘Comet’ => true,
‘Cupid’ => true, ‘Blitzen’ => true,
‘Rudolph’ => true, ‘Olive’ => true}

p (elves | reindeer)[‘Comet’] # ==> true
p (elves[‘Comet’] | reindeer[‘Comet’]) # ==> true

Eagerly eyeing the ruby-shaped package under tree,
Joel

Hash#union, also Hash#| (not Hash#^).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.12.22 at 20.57.15

···

On Mon, 23 Dec 2002 08:05:00 +0900, Yukihiro Matsumoto wrote:

In message “Re: Hash#update” > on 02/12/23, Mark Slagell ms@iastate.edu writes:

As you say, non-commutative ‘+’ is common enough in programming.
Array#+ is another example. Consider also that there is an
intuitive |temporal sense to these expressions. In (hash1 +
hash2), we are starting with hash1. After hash2 is brought into
the picture, the newer bits would be expected to take precedence.
I don’t really see a problem here.
No information lost by String#+ or Array#+, but Hash#update may
lose the value. That’s why I rejected the name “+”. It is possible
to provide the non destructive “update” under different name. Good
name?

Austin Ziegler wrote:

···

On Mon, 23 Dec 2002 08:05:00 +0900, Yukihiro Matsumoto wrote:

In message “Re: Hash#update” >>on 02/12/23, Mark Slagell ms@iastate.edu writes:

As you say, non-commutative ‘+’ is common enough in programming.
Array#+ is another example. Consider also that there is an
intuitive |temporal sense to these expressions. In (hash1 +
hash2), we are starting with hash1. After hash2 is brought into
the picture, the newer bits would be expected to take precedence.
I don’t really see a problem here.
No information lost by String#+ or Array#+, but Hash#update may
lose the value. That’s why I rejected the name “+”. It is possible
to provide the non destructive “update” under different name. Good
name?

Hash#union, also Hash#| (not Hash#^).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.12.22 at 20.57.15

Yes. Can we have both?

–Mark

Hi,

···

In message “Re: Hash#update” on 02/12/23, Austin Ziegler austin@halostatue.ca writes:

No information lost by String#+ or Array#+, but Hash#update may
lose the value. That’s why I rejected the name “+”. It is possible
to provide the non destructive “update” under different name. Good
name?

Hash#union, also Hash#| (not Hash#^).

Is it OK for you to lose values by the “union” method? Not for me.

						matz.

How about Hash#merge and Hash#merge! ?

martin

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message “Re: Hash#update” > on 02/12/23, Austin Ziegler austin@halostatue.ca writes:

No information lost by String#+ or Array#+, but Hash#update may
lose the value. That’s why I rejected the name “+”. It is possible
to provide the non destructive “update” under different name. Good
name?

Hash#union, also Hash#| (not Hash#^).

Is it OK for you to lose values by the “union” method? Not for me.

Actually, it is. Union – as in a set union – only keeps one
instance of the key. In this instance, I would expect that:

h1 = { 1 => 2, 2 => 3, 3 => 4 }
h2 = { 1 => 5, 4 => 6, 3 => 7 }
h3 = h1 | h2 # { 1 => 5, 2 => 3, 3 => 7, 4 => 6 }
h4 = h2 | h1 # { 1 => 2, 2 => 3, 3 => 4, 4 => 6 }

I would definitely NOT expect loss of values in “+” or “union all”
(to use the SQL term).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.12.24 at 09.40.12

···

On Tue, 24 Dec 2002 11:47:36 +0900, Yukihiro Matsumoto wrote:

In message “Re: Hash#update” on 02/12/23, Austin Ziegler > austin@halostatue.ca writes:

No information lost by String#+ or Array#+, but Hash#update may
lose the value. That’s why I rejected the name “+”. It is
possible to provide the non destructive “update” under different
name. Good name?
Hash#union, also Hash#| (not Hash#^).
Is it OK for you to lose values by the “union” method? Not for me.

[…]

Is it OK for you to lose values by the “union” method? Not for me.

How about Hash#merge and Hash#merge! ?

Or,
Hash#update (destructive)
Hash#updated_by (non-destructive)

If so, could we keep Hash#| as well? After all, the resulting hash is
based upon the union of the original keys.

– Mark