equality

Recommended way to implement __eq__ and __hash__

倖福魔咒の 提交于 2019-12-10 04:06:50
问题 The python documentation mentions that if you override __eq__ and the object is immutable, you should also override __hash__ in order for the class to be properly hashable. In practice, when I do this I often end up with code like class MyClass(object): def __init__(self, a, b): self.a = a self.b = b def __eq__(self, other): if type(other) is type(self): return (self.a == other.a) and (self.b == other.b) else: return False def __hash__(self): return hash((self.a, self.b)) This is somewhat

Python3 Determine if two dictionaries are equal [duplicate]

风流意气都作罢 提交于 2019-12-10 03:34:25
问题 This question already has answers here : Comparing two dictionaries and checking how many (key, value) pairs are equal (23 answers) Closed last year . This seems trivial, but I cannot find a built-in or simple way to determine if two dictionaries are equal. what I want is: a = {'foo': 1, 'bar': 2} b = {'foo': 1, 'bar': 2} c = {'bar': 2, 'foo': 1} b = {'foo': 2, 'bar': 1} e = {'foo': 1, 'bar': 2, 'baz':3} f = {'foo': 1} equal(a, b) # True equal(a, c) # True - order does not matter equal(a, d)

Equality testing without explicit proof that data constructors are injective

夙愿已清 提交于 2019-12-10 02:26:35
问题 Is it possible to define a simple syntactic notion of equality (similar to what GHC might automatically derive as the Eq instance for a Haskell 98 type), without either explicitly proving that each data constructor is injective, or doing something analogous, such as defining the retraction of each constructor and using cong ? In other words, is it possible to exploit the injectivity of data constructors more directly, rather than having to introduce one auxiliary function per constructor? The

Implementing a geographic coordinate class: equality comparison

血红的双手。 提交于 2019-12-10 02:16:05
问题 I 'm integrating a geographic coordinate class from CodePlex to my personal "toolbox" library. This class uses float fields to store latitude and longitude. Since the class GeoCoordinate implements IEquatable<GeoCoordinate> , I habitually wrote the Equals method like so: public bool Equals(GeoCoordinate other) { if (other == null) { return false; } return this.latitude == other.latitude && this.longitude == other.longitude; } At this point I stopped and considered that I 'm comparing floating

refl in agda : explaining congruence property

不想你离开。 提交于 2019-12-09 19:21:35
问题 With the following definition of equality, we have refl as constructor data _≡_ {a} {A : Set a} (x : A) : A → Set a where refl : x ≡ x and we can prove that function are congruent on equality cong : ∀ { a b} { A : Set a } { B : Set b } (f : A → B ) {m n} → m ≡ n → f m ≡ f n cong f refl = refl I am not sure I can parse what is going on exactly here. I think we are pattern matching refl on hidden parameters : if we replace the first occurence by refl by another identifier, we get a type error.

Why aren't IStructuralEquatable and IStructuralComparable generic?

放肆的年华 提交于 2019-12-09 16:28:20
问题 System.Collections.IStructuralEquatable and System.Collections.IStructuralComparable were added in .NET 4, but why aren't they generic, like IEquatable<T> and IComparable<T> ? 回答1: The example on MSDN gives part of the answer here; it seems to be useful for heterogeneous equality, rather than homogeneous equality - i.e. for testing whether two objects (/values) of potentially different types should be considered equal. In such scenarios, it is extremely likely that the calling code is dealing

2 NSDates that should be equal aren't?

白昼怎懂夜的黑 提交于 2019-12-09 13:53:07
问题 I'm using the JSON library from Stig Brautaset(http://code.google.com/p/json-framework) and I need to serialize an NSDate. I was considering converting it into a string before JSONifying it, however, I ran into this weird behavior: Why aren't these NSDates considered equal? NSDate *d = [[NSDate alloc] init]; NSDate *dd = [NSDate dateWithString:[d description]]; NSLog(@"%@", d); NSLog(@"%@", dd); if( [d isEqualToDate:dd] ){ NSLog(@"Yay!"); } 回答1: When you describe the original date object you

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

爱⌒轻易说出口 提交于 2019-12-09 08:25:35
问题 When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have the same result. So what's the difference between using IEqualityComparer and Equals/GethashCode Override? Two Examples: class Customer { public string name; public int age; public Customer(string n, int a) { this.age = a; this.name = n; } public

What is the difference between equivalence and equality?

限于喜欢 提交于 2019-12-09 05:06:56
问题 What is the difference between equivalence and equality in C++? There is a very similar question here. However, this question is tagged with math, while I am interested in the meaning in C++ context. To see the terms in context: Scott Meyers uses them in an example in this video. 回答1: On cppreference.com i found the following quote: For the types that are both EqualityComparable and LessThanComparable, the C++ standard library makes a distinction between equality, which is the value of the

PHP: Testing whether three variables are equal

。_饼干妹妹 提交于 2019-12-09 05:01:38
问题 I've never come across this before, but how would you test whether three variables are the same? The following, obviously doesn't work but I can't think of an elegant (and correct) way to write the following: if ($select_above_average === $select_average === $select_below_average) { } 回答1: if ((a == b) && (b == c)) { ... they're all equal ... } by the transitive relation 回答2: $values = array($select_above_average, $select_average, $select_below_average); if(count(array_unique($values)) === 1)