Hash#+?

I think that there should be a Hash#+ method, aking to Array#+.

array = [ “Daniel” ]
array + [ “Carrera” ] # -> [ “Daniel”, “Carrera” ]

hash = { “first” => “Daniel” }
hash + { “last” => “Carrera” } # -> {“first” => “Daniel”,
# “last” => “Carrera” }

Ditto for Hash#-.

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Hi –

I think that there should be a Hash#+ method, aking to Array#+.

array = [ “Daniel” ]
array + [ “Carrera” ] # → [ “Daniel”, “Carrera” ]

hash = { “first” => “Daniel” }
hash + { “last” => “Carrera” } # → {“first” => “Daniel”,
# “last” => “Carrera” }

Ditto for Hash#-.

Have a look at the thread starting at http://www.ruby-talk.org/59777
for some recent discussion of this. In 59794 Matz says:

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?

David

···

On Thu, 23 Jan 2003, Daniel Carrera wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

David, what value can be lost? i don’t understand.

···

On Wednesday 22 January 2003 06:52 pm, dblack@candle.superlink.net wrote:

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?


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

hash1 = { “key1” => “foo”, “key2” => “bar” }
hash2 = { “key1” => “baz”, “key3” => “blah” }

hash1 + hash2 # → What is the value for “key1”?

I like the idea of Hash#| or Hash#or. It is simply an ‘or’ function.
Whichever comes first has precedence:

hash1 | hash2 # → “key1” => “foo”
hash2 | hash1 # → “key1” => “baz”

···

On Thu, Jan 23, 2003 at 11:14:08AM +0900, Tom Sawyer wrote:

On Wednesday 22 January 2003 06:52 pm, dblack@candle.superlink.net wrote:

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?

David, what value can be lost? i don’t understand.


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Hi,

···

In message “Re: Hash#+ ?” on 03/01/23, Tom Sawyer transami@transami.net writes:

On Wednesday 22 January 2003 06:52 pm, dblack@candle.superlink.net wrote:

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?

David, what value can be lost? i don’t understand.

What if two hashes have same keys with different values?

						matz.

David, what value can be lost? i don’t understand.

hash1 = { “key1” => “foo”, “key2” => “bar” }
hash2 = { “key1” => “baz”, “key3” => “blah” }
hash1 + hash2 # → What is the value for “key1”?

i see what youre saying. indeed, + is not really the right method for this.

I like the idea of Hash#| or Hash#or. It is simply an ‘or’ function.
Whichever comes first has precedence:

hash1 | hash2 # → “key1” => “foo”
hash2 | hash1 # → “key1” => “baz”

i posted this RCR on Ruby Garden but i expect the second hash to have
precedence. i.e. | is defined as a non in-place #update.

···

On Wednesday 22 January 2003 07:20 pm, Daniel Carrera wrote:


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Hi,

···

In message “Re: Hash#+ ?” on 03/01/23, Tom Sawyer transami@transami.net writes:

I like the idea of Hash#| or Hash#or. It is simply an ‘or’ function.
Whichever comes first has precedence:

hash1 | hash2 # → “key1” => “foo”
hash2 | hash1 # → “key1” => “baz”

i posted this RCR on Ruby Garden but i expect the second hash to have
precedence. i.e. | is defined as a non in-place #update.

So you guys are OK for value losing “or” or “|” methods?

						matz.

Just an idea, can Hash#| check to see if the data types of the conflicting keys already HAVE an
‘|’ method (assuming the data types are the same, of course)?

That way, we can do something like this…

Hash1 is {“key1”=>[“val1”]}
Hash2 is {“key1”=>[“val2”]}

Hash1 | Hash2 gives {“key1”=>[“val1”, “val2”]}

Or should that be a different method altogether?

Jason

···

— Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message “Re: Hash#+ ?” > on 03/01/23, Tom Sawyer transami@transami.net writes:

I like the idea of Hash#| or Hash#or. It is simply an ‘or’ function.
Whichever comes first has precedence:

hash1 | hash2 # → “key1” => “foo”
hash2 | hash1 # → “key1” => “baz”

i posted this RCR on Ruby Garden but i expect the second hash to have
precedence. i.e. | is defined as a non in-place #update.

So you guys are OK for value losing “or” or “|” methods?

  					matz.

yes. a number of times in my experience a non in-place version of update would
have been handy.

···

On Wednesday 22 January 2003 08:49 pm, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Hash#+ ?” > > on 03/01/23, Tom Sawyer transami@transami.net writes:

I like the idea of Hash#| or Hash#or. It is simply an ‘or’ function.
Whichever comes first has precedence:

hash1 | hash2 # → “key1” => “foo”
hash2 | hash1 # → “key1” => “baz”

i posted this RCR on Ruby Garden but i expect the second hash to have
precedence. i.e. | is defined as a non in-place #update.

So you guys are OK for value losing “or” or “|” methods?

  					matz.


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Hi,

···

In message “Re: Hash#+ ?” on 03/01/23, Jason Persampieri jason@persampieri.net writes:

Just an idea, can Hash#| check to see if the data types of the conflicting keys already HAVE an
‘|’ method (assuming the data types are the same, of course)?

FYI, “update” can resolve conflict using blocks.

hash1.update(hash2) {|key,v1,v2| …}

but operators don’t take blocks.

						matz.

Hi,

···

In message “Re: Hash#+ ?” on 03/01/23, Tom Sawyer transami@transami.net writes:

So you guys are OK for value losing “or” or “|” methods?

yes. a number of times in my experience a non in-place version of update would
have been handy.

Handiness is not my point. I know it must be handy.
I care about names.

						matz.

What’s wrong with ‘hash3 = hash1.dup.update(hash2)’ ? And I’d like to
know how a non in-place version of update is used that frequently.
Would you show us some (live) examples?

···

At Thu, 23 Jan 2003 13:38:01 +0900, Tom Sawyer wrote:

So you guys are OK for value losing “or” or “|” methods?

  					matz.

yes. a number of times in my experience a non in-place version of update would
have been handy.


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“It went right by me – At the time it went over my head
I was looking out the window… I should have looked at your face instead”

“yes.”

···

On Wednesday 22 January 2003 09:42 pm, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Hash#+ ?” > > on 03/01/23, Tom Sawyer transami@transami.net writes:

So you guys are OK for value losing “or” or “|” methods?

yes. a number of times in my experience a non in-place version of update
would have been handy.

Handiness is not my point. I know it must be handy.
I care about names.

  					matz.


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

well, i don’t have any LIVE examples b/c hash#| dosen’t exist, now does it!
:slight_smile: but i will give you some places i could have used it as i come across
them. sorry, i don’t have time to go dig them up right now.

···

On Sunday 02 February 2003 11:25 pm, Akinori MUSHA wrote:

At Thu, 23 Jan 2003 13:38:01 +0900, > > Tom Sawyer wrote:

So you guys are OK for value losing “or” or “|” methods?

  					matz.

yes. a number of times in my experience a non in-place version of update
would have been handy.

What’s wrong with ‘hash3 = hash1.dup.update(hash2)’ ? And I’d like to
know how a non in-place version of update is used that frequently.
Would you show us some (live) examples?


tom sawyer, aka transami
transami@transami.net

i second that. the ‘normal’ semantics of ‘|’ are preserved, IMHO, iff the
first hash wins

-a

···

On Thu, 23 Jan 2003, Tom Sawyer wrote:

On Wednesday 22 January 2003 09:42 pm, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Hash#+ ?” > > > > on 03/01/23, Tom Sawyer transami@transami.net writes:

So you guys are OK for value losing “or” or “|” methods?

yes. a number of times in my experience a non in-place version of update
would have been handy.

Handiness is not my point. I know it must be handy.
I care about names.

  					matz.

“yes.”

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

by the way, whats wrong with ‘arr3 = arr1.dup.concat(arr2)’ ?

···

On Sunday 02 February 2003 11:25 pm, Akinori MUSHA wrote:

What’s wrong with ‘hash3 = hash1.dup.update(hash2)’ ? And I’d like to
know how a non in-place version of update is used that frequently.
Would you show us some (live) examples?


tom sawyer, aka transami
transami@transami.net

glad to be in agreement. i only disagree about the ‘normal’ semantics. where
are you deriving that from? for my part i think the second hash should win
such that:

hash1 = hash1 | hash2	

has the same effect as:

hash1.update(hash2)
···

On Wednesday 22 January 2003 11:43 pm, ahoward wrote:

i second that. the ‘normal’ semantics of ‘|’ are preserved, IMHO, iff the
first hash wins

-a


tom sawyer, aka transami
transami@transami.net

We have live examples that show that Array#+ is useful. Try grepping
the source tree for /+=? *[/. Another reason for Array#+ is that
arrays are stackable and a + operator matches our expectation and an
analogy to String#+.

My question is: would it be so a popular demand to add a new method
for Hash? We have been living without it and a.dup.update(b) or
{}.update(a).update(b) will do the job if you need the functionality.
I’m not really against the idea, but just curious as to how it is
going to be used.

···

At Mon, 3 Feb 2003 21:31:53 +0900, Tom Sawyer wrote:

On Sunday 02 February 2003 11:25 pm, Akinori MUSHA wrote:

What’s wrong with ‘hash3 = hash1.dup.update(hash2)’ ? And I’d like to
know how a non in-place version of update is used that frequently.
Would you show us some (live) examples?

by the way, whats wrong with ‘arr3 = arr1.dup.concat(arr2)’ ?


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“It went right by me – At the time it went over my head
I was looking out the window… I should have looked at your face instead”

I agree- second hash should win.

Maybe he meant ‘||’ ?

···

On Thursday 23 January 2003 01:49 am, Tom Sawyer wrote:

glad to be in agreement. i only disagree about the ‘normal’ semantics.
where are you deriving that from? for my part i think the second hash
should win such that:


Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com

‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams

0 | 0 → 0
0 | 1 → 1
1 | 0 → 1
1 | 1 → 1

in the last case it would be crazy to design something which continued
evaluating after determining that the ‘first’ bit was one, since at that point
the outcome could only be 1

to me it jives with idea of a bitmask since bits may only ever be turned on,
not off. eg :

irb(main):001:0> 0b01 | 0b10
3
irb(main):002:0> 0b10 | 0b11
3

to me your semantics (second wins) seems to point to a

hash0 << hash1

operator to me - to be consistent with the rest of ruby.

but i don’t really case so long as a ‘|’ comes out of it. this makes me think
of an ‘^’ (xor) operator which would have the semantics "return a new hash
with those keys appear in one, or the other, hash but not those keys which
appear in both. but how to resolve the conflicts…

···

On Thu, 23 Jan 2003, Tom Sawyer wrote:

On Wednesday 22 January 2003 11:43 pm, ahoward wrote:

i second that. the ‘normal’ semantics of ‘|’ are preserved, IMHO, iff the
first hash wins

-a

glad to be in agreement. i only disagree about the ‘normal’ semantics. where
are you deriving that from? for my part i think the second hash should win
such that:

hash1 = hash1 | hash2

has the same effect as:

hash1.update(hash2)

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================