Project

General

Profile

Actions

Feature #14105

closed

Introduce xor as alias for Set#^

Added by ana06 (Ana Maria Martinez Gomez) over 6 years ago. Updated over 5 years ago.

Status:
Feedback
Target version:
-
[ruby-core:83761]

Description

Not sure if I should also add feautures for the Set class here or if I should do it somewhere else.

I would like to add a more readable method for the exclusive or, currently as ^ in set. It is in both mathematics, hardware and many other programming languages known as xor, so I think this will help to use it.


Files

firefox.desktop (528 Bytes) firefox.desktop ana06 (Ana Maria Martinez Gomez), 11/14/2017 12:36 PM

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #14097: Add union and difference to ArrayClosedmatz (Yukihiro Matsumoto)Actions
Actions #1

Updated by ana06 (Ana Maria Martinez Gomez) over 6 years ago

  • Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN)
  • Tracker changed from Bug to Feature

Updated by jeremyevans0 (Jeremy Evans) over 6 years ago

^ is used as the exclusive-or operator in many other programming languages including C, C++, C#, D, Java, Perl, PHP and Python.

If a user wants to use xor instead of ^, they can always alias it manually:

class Set
  alias xor ^
end

In general I prefer that users add their own aliases. I don't think Ruby should add aliases unless the current method names are so bad as to be misleading, and I don't believe that to be the case here.

Note that the following core classes also use ^ for exclusive-or:

  • Integer
  • FalseClass
  • TrueClass
  • NilClass

One counterpoint is that Set already aliases & to intersection and | to union, so adding xor would be consistent in that sense.

Updated by shevegen (Robert A. Heiler) over 6 years ago

I am neutral on this so either way is fine by me. I very rarely
use Set so I suppose I would not even notice any change in the
first place. :)

I may be wrong but the amount of approved feature additions appears
to have slowed down in the last 2-3 weeks or so. There could be many
reasons for this but I assume that one is the upcoming x-mas release
in about a month (which, I assume, means that bug fixes probably have
a higher priority at the moment; at the least I see nobu being busy
doing lots of changes primarily related to bug fixes).

Ultimately you only have to convince matz. :)

It may help to showcase specific examples, e. g. the exact different
for Set#xor rather than Set#^. (I assume that one practical advantage
for the ^ is because it may be shorter to type, but again, I really
have no particular pro or con opinion here, just pointing out a few
not so relevant things. :P)

Updated by ana06 (Ana Maria Martinez Gomez) about 6 years ago

@jeremyevans0 (Jeremy Evans)

Note that the following core classes also use ^ for exclusive-or:

Integer
FalseClass
TrueClass
NilClass

That's a good point. Maybe we should consider adding the alias to the 4 classes. My idea with this was trying to make it more readable and easy to use, which Ruby does for many other things.

Actions #5

Updated by matz (Yukihiro Matsumoto) almost 6 years ago

Updated by naruse (Yui NARUSE) almost 6 years ago

  • Status changed from Open to Assigned
  • Assignee set to knu (Akinori MUSHA)

Updated by knu (Akinori MUSHA) almost 6 years ago

I'm afraid aliasing ^ to xor is not like aliasing & to intersection and | to union, because the latter two are the terms defined in the set theory whereas xor is (apparently) not.

Updated by gotoken (Kentaro Goto) almost 6 years ago

knu (Akinori MUSHA) wrote:

I'm afraid aliasing ^ to xor is not like aliasing & to intersection and | to union, because the latter two are the terms defined in the set theory whereas xor is (apparently) not.

Yep, that is usually called the symmetric difference in elementary set theory. Even so, some text books introduce the relation between them as

a△b = {x | x∈a ⊕ x∈b}

where △ and ⊕ stand for symmetric difference and XOR respectively.

By the way I recently wrote this set theoretical operation to find all uncommon files in two file collections then I reminded this feature request :)

Updated by knu (Akinori MUSHA) almost 6 years ago

gotoken (Kentaro Goto) wrote:

By the way I recently wrote this set theoretical operation to find all uncommon files in two file collections then I reminded this feature request :)

That sounds interesting. I don't think I actually had a use case for Set#^ in mind when I implemented it, so I'd appreciate if you could share some real use cases for it. 😄

Actions #10

Updated by knu (Akinori MUSHA) over 5 years ago

  • Status changed from Assigned to Feedback
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0