Forum: Ruby-core [ruby-trunk - Feature #6225][Open] Hash#+

Posted by Thomas Sawyer (7rans)
on 2012-03-30 01:19
(Received via mailing list)
Issue #6225 has been reported by trans (Thomas Sawyer).

----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by mame (Yusuke Endoh) (Guest)
on 2012-03-30 01:37
(Received via mailing list)
Issue #6225 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)

Both String#+ and Array#+ delete no information, but
Hash#merge deletes duplicate fields.
I have heard it is the reason, if I recall.

--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25425

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by rosenfeld (Rodrigo Rosenfeld Rosas) (Guest)
on 2012-03-30 02:40
(Received via mailing list)
Issue #6225 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


This argument really doesn't buy me. Groovy allows: [key1: 'value1', 
another: 1] + [key2: 'value2', another: 2] == [key1: 'value1', another: 
2, key2: 'value2'].

I think this is pretty readable as + is also used to denotate unions. I 
don't think anyone would expect any behavior different from that.

Maybe someone could argue that the result could also mean [key1: 
'value1', another: [1, 2], key2: 'value2'] but I think *that* would be 
really strange.
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25435

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by shyouhei (Shyouhei Urabe) (Guest)
on 2012-03-30 04:30
(Received via mailing list)
Issue #6225 has been updated by shyouhei (Shyouhei Urabe).


I object.  No binary operations shall be called + unless the operation 
is symmetric.
For historical reasons there are some asymmetric +s in Ruby, but that is 
not a indulgence for you to add more.
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25438

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by rosenfeld (Rodrigo Rosenfeld Rosas) (Guest)
on 2012-03-30 04:43
(Received via mailing list)
Issue #6225 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


In other words you don't like that {a: 1} + {a: 2} != {a: 2} + {a: 1}

But I really think programming is different from mathematics and I don't 
think that the fact that a + b != b + a would be enough reason for 
avoiding the convenient operator por  "b merged to a" (a + b).

Not that I really do care that much about this feature request as I 
don't see any problem on writing a.merge(b) anyway... I just don't see 
any problems either for having "a + b == a.merge(b)".
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25439

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by Thomas Sawyer (7rans)
on 2012-03-30 04:49
(Received via mailing list)
Issue #6225 has been updated by trans (Thomas Sawyer).


"No binary operations shall be called + unless the operation is 
symmetric."

Why?

Also what do you mean by symmetric? Do you mean commutative? I point out 
that neither Array#+ or String#+ is really commutative either because 
order is significant.

Also, I don't know why you say "indulgence". It's a simple convenience 
as a means of writing concise, yet readable, code.


----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25440

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by shyouhei (Shyouhei Urabe) (Guest)
on 2012-03-30 05:07
(Received via mailing list)
Issue #6225 has been updated by shyouhei (Shyouhei Urabe).


Sorry for my bad English, I dodn't intend to attack you.

Anyway there has been a long discussion around +s in programming 
languages.  For instance Perl uses dot to concatenate strings and avoid 
+s to concatename strings and/or arrays.  I see they are much more 
mature than us in this area.  Functional languages like Haskell also 
avoid defining + onto non-abelian groups.
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25441

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by "duerst (Martin Dürst)" <duerst@it.aoyama.ac.jp> (Guest)
on 2012-03-30 08:02
(Received via mailing list)
Issue #6225 has been updated by duerst (Martin Dürst).


In common sense, * is also commutative. But of course, for matrix 
multiplication, it's not.

Also, + is used in many fields of mathematics. I'm not a mathematician, 
but I very strongly doubt that it's commutative in all these cases. (see 
e.g. http://arxiv.org/abs/1003.2081 for an example)

What's much more important is whether the + for Hash fits the general 
image a Ruby programmer has for +. I'm not exactly sure about this, but 
the parallel with Array is not too bad.
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25448

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by matz (Yukihiro Matsumoto) (Guest)
on 2012-03-30 09:56
(Received via mailing list)
Issue #6225 has been updated by matz (Yukihiro Matsumoto).


I myself do not care whether + to be symmetric or not.  I care about key 
conflict.
Since conflicting cause value lost, I am not positive about making + 
alias to #merge.


Matz.

----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25456

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by naruse (Yui NARUSE) (Guest)
on 2012-03-30 10:56
(Received via mailing list)
Issue #6225 has been updated by naruse (Yui NARUSE).


shyouhei (Shyouhei Urabe) wrote:
> Anyway there has been a long discussion around +s in programming languages.  For 
instance Perl uses dot to concatenate strings and avoid +s to concatename strings 
and/or arrays.  I see they are much more mature than us in this area.  Functional 
languages like Haskell also avoid defining + onto non-abelian groups.

On Perl, it is because for
 perl -e'print "1" + "2"' #=> 3
 perl -e'print "1" . "2"' #=> 12

Not because of symmetry.
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25459

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by jacksonwillis (Jackson Willis) (Guest)
on 2012-04-08 05:55
(Received via mailing list)
Issue #6225 has been updated by jacksonwillis (Jackson Willis).


Would it be better to use `Hash#<<' instead of `Hash#+'?
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25713

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by Thomas Sawyer (7rans)
on 2012-04-12 16:36
(Received via mailing list)
Issue #6225 has been updated by trans (Thomas Sawyer).


@jacksonwillis #<< makes sense as an alias for #update, not #merge. 
However I use Hash#<< with this meaning:

  def <<(array)
    raise if array.size != 2
    self[array.first] = array.last
  end

There are historical reasons for that definition. But the two can be 
combined:

  def <<(object)
    case object
    when Array
      raise if object.size != 2
      self[object.first] = object.last
    else
      update(object)
    end
  end

----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25857

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-04-12 21:01
(Received via mailing list)
Issue #6225 has been updated by alexeymuranov (Alexey Muranov).


Martin, it is common to use * for both commutative and non-commutative 
operations but + for only commutative.

But i am more in favor of Matz's argument, because i didn't bother 
myself about the fact that string addition is non -commutative.

duerst (Martin Dürst) wrote:
> In common sense, * is also commutative. But of course, for matrix 
multiplication, it's not.
>
> Also, + is used in many fields of mathematics. I'm not a mathematician, but I 
very strongly doubt that it's commutative in all these cases. (see e.g. 
http://arxiv.org/abs/1003.2081 for an example)
>
> What's much more important is whether the + for Hash fits the general image a 
Ruby programmer has for +. I'm not exactly sure about this, but the parallel with 
Array is not too bad.


----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25861

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-04-12 22:28
(Received via mailing list)
Issue #6225 has been updated by alexeymuranov (Alexey Muranov).


How about `|` instead of `+` ??
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25863

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-04-13 07:48
(Received via mailing list)
Issue #6225 has been updated by alexeymuranov (Alexey Muranov).


I would like to give an algebraic counterpart to Matz's objection: 
`merge` is not injective in second argument: a.merge(b) == a.merge(c) 
does not imply b == c, but most uses of `+` are injective in each of the 
arguments.

However, `Set#+` is an exception to this rule, it seems that it is 
equivalent to `Set#|`, isn't it?
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-25873

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by fahmisetiawand (Fahmi Setiawan) (Guest)
on 2012-10-03 11:38
(Received via mailing list)
Issue #6225 has been updated by fahmisetiawand (Fahmi Setiawan).


http://alkian.blogspot.com/2012/09/cara-menyembuhk...
http://alkian.blogspot.com/2012/09/keretamini-kere...
http://alkian.blogspot.com/2012/10/kata-kata-mutia...
http://alkian.blogspot.com/2012/08/personil-coboy-...
http://alkian.blogspot.com/2012/10/hasil-dan-klase...
----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-29979

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Posted by naruse (Yui NARUSE) (Guest)
on 2012-10-25 08:53
(Received via mailing list)
Issue #6225 has been updated by naruse (Yui NARUSE).

Target version changed from 2.0.0 to next


----------------------------------------
Feature #6225: Hash#+
https://bugs.ruby-lang.org/issues/6225#change-31489

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next


Strings and Arrays can be combined with #+. I don't see any reason not 
to allow Hashes to do so as well.

  class Hash
    alias :+ :merge
  end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.