equivalence

Overloading equivalence (==) operator for custom class in Swift

懵懂的女人 提交于 2019-12-01 02:32:35
Is it possible to overload equivalence (==) operator for a custom class inside that custom class. However I know that it is possible to have this operator overloaded outside class scope. Appreciate any sample code. Thanks in advance. Add global functions. For example: class CustomClass { var id = "my id" } func ==(lhs: CustomClass, rhs: CustomClass) -> Bool { return lhs == rhs } func !=(lhs: CustomClass, rhs: CustomClass) -> Bool { return !(lhs == rhs) } To conform Equatable protocol in Swift 2 class CustomClass: Equatable { var id = "my id" } func ==(left: CustomClass, right: CustomClass) ->

How should one proceed to prove (or find) if two regular expressions are same or equivalent?

一曲冷凌霜 提交于 2019-11-30 07:18:05
For example, in an assignment given to me, we were asked to find out if two regular expressions are equal or not. (a+b+c)* and ((ab)**c*)* My question is how is one supposed to do that? If I draw the transition graphs for both and then run a few strings through it and show that both of the TGs are able to accept it, is that a sufficient proof ? If not, how do I do it? Is there a mathematical/axiomatic approach towards this? Thanks in advance. EDIT: There is another thing that I'd like to clear which is kind of related to this question. Are the two FAs depicted in the photo below the same? i.e.

Is JavaScript's double equals (==) always symmetric?

我与影子孤独终老i 提交于 2019-11-30 05:39:28
There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see " JavaScript equality transitivity is weird ." However, are there any cases in which == isn't symmetric ? That is, where a == b is true and b == a is false ? SLaks In Javascript, == is always symmetric . The spec says : NOTE 2 The equality operators maintain the following invariants: A != B is equivalent to !(A == B) . A == B is equivalent to B == A , except in the order of evaluation of A and B . It's supposed to be symmetric. However, there is an asymmetric case in some versions of

Why does `if None.__eq__(“a”)` seem to evaluate to True (but not quite)?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 19:28:58
If you execute the following statement in Python 3.7, it will (from my testing) print b : if None.__eq__("a"): print("b") However, None.__eq__("a") evaluates to NotImplemented . Naturally, "a".__eq__("a") evaluates to True , and "b".__eq__("a") evaluates to False . I initially discovered this when testing the return value of a function, but didn't return anything in the second case -- so, the function returned None . What's going on here? This is a great example of why the __dunder__ methods should not be used directly as they are quite often not appropriate replacements for their equivalent

How should one proceed to prove (or find) if two regular expressions are same or equivalent?

不羁的心 提交于 2019-11-29 09:30:01
问题 For example, in an assignment given to me, we were asked to find out if two regular expressions are equal or not. (a+b+c)* and ((ab)**c*)* My question is how is one supposed to do that? If I draw the transition graphs for both and then run a few strings through it and show that both of the TGs are able to accept it, is that a sufficient proof ? If not, how do I do it? Is there a mathematical/axiomatic approach towards this? Thanks in advance. EDIT: There is another thing that I'd like to

Why does `if None.__eq__(“a”)` seem to evaluate to True (but not quite)?

為{幸葍}努か 提交于 2019-11-28 14:57:00
问题 If you execute the following statement in Python 3.7, it will (from my testing) print b : if None.__eq__("a"): print("b") However, None.__eq__("a") evaluates to NotImplemented . Naturally, "a".__eq__("a") evaluates to True , and "b".__eq__("a") evaluates to False . I initially discovered this when testing the return value of a function, but didn't return anything in the second case -- so, the function returned None . What's going on here? 回答1: This is a great example of why the __dunder__

Dictionary Keys.Contains vs. ContainsKey: are they functionally equivalent?

独自空忆成欢 提交于 2019-11-28 00:41:09
I am curious to know if these two are functionally equivalent in all cases. Is it possible that by changing the dictionary's default comparator that these two would be functionally different? Also, isn't Keys.Contains almost guaranteed to be slower? These two functions do exactly the same thing. Keys.Contains exists because Keys is an ICollection<TKey> , which defines a Contains method. The standard Dictionary<TKey, TValue>.KeyCollection implementation (the class, not the interface) defines it as bool ICollection<TKey>.Contains(TKey item){ return dictionary.ContainsKey(item); } Since it's

Boxed Primitives and Equivalence

老子叫甜甜 提交于 2019-11-27 23:35:11
So I was asked this question today. Integer a = 3; Integer b = 2; Integer c = 5; Integer d = a + b; System.out.println(c == d); What will this program print out? It returns true. I answered it will always print out false because of how I understood auto (and auto un) boxing. I was under the impression that assigning Integer a = 3 will create a new Integer(3) so that an == will evaluate the reference rather then the primitive value. Can anyone explain this? Peter Štibraný Boxed values between -128 to 127 are cached. Boxing uses Integer.valueOf method, which uses the cache. Values outside the

What are uses of the C++ construct “placement new”?

和自甴很熟 提交于 2019-11-27 19:51:56
I just learned about the C++ construct called "placement new". It allows you to exactly control where a pointer points to in memory. It looks like this: #include <new> // Must #include this to use "placement new" #include "Fred.h" // Declaration of class Fred void someCode() { char memory[sizeof(Fred)]; void* place = memory; Fred* f = new(place) Fred(); // Create a pointer to a Fred(), // stored at "place" // The pointers f and place will be equal ... } (example from C++ FAQ Lite ) In this example, the this pointer of Fred will be equal to place . I've seen it used in our team's code once or

Java char is also an int?

谁说我不能喝 提交于 2019-11-27 16:08:42
I was trying to get some code done for class: public int getValue(char value) { if (value == 'y') return this.y; else if (value == 'x') return this.x; Since I might not be able to return anything in the end, it told me to do this at the end: return value; This surprised me because the return type for the method was of type int . Yet, it was telling me to return a char ! I'm using eclipse, and accustomed to the endless number of warnings and stuff, this was a major surprise. So, is a char really an int ? Why is this happening? The Java Language Specification states When a return statement with