equality

Why is this code throwing an InvalidOperationException?

流过昼夜 提交于 2019-12-03 13:08:26
I think that my code should make the ViewBag.test property equal to "No Match" , but instead it throws an InvalidOperationException . Why is this? string str = "Hello1,Hello,Hello2"; string another = "Hello5"; string retVal = str.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .First(p => p.Equals(another)); if (str == another) { ViewBag.test = "Match"; } else { ViewBag.test = "No Match"; //this does not happen when it should } As you can see here , the First method throws an InvalidOperationException when the sequence on which it is called is empty. Since no element of the

`Refl` thing in Calculus of Constructions?

本小妞迷上赌 提交于 2019-12-03 12:32:41
问题 In languages such as Agda , Idris , or Haskell with type extensions, there is a = type sort of like the following data a :~: b where Refl :: a :~: a a :~: b means that a and b are the same. Can such a type be defined in the calculus of constructions or Morte (which is programming language based on the calculus of construction)? 回答1: The standard Church-encoding of a :~: b in CoC is: (a :~: b) = forall (P :: * -> * -> *). (forall c :: *. P c c) -> P a b Refl being Refl a :: a :~: a Refl a = \

Best practice to choose fields for equals() implementation

大城市里の小女人 提交于 2019-12-03 11:36:22
问题 When writing unit-tests, I often face the situation when equals() for some object in tests -- in assertEquals -- should work differently from how it works in actual environment. Take for example some interface ReportConfig . It has id and several other fields. Logically, one config equals to another one when their id s match. But when it comes to testing some specific implementation, say, XmlReportConfig , obviously I want to match all fields. One solution is not to use equals in tests and

Is it possible to define equality for named types/structs?

与世无争的帅哥 提交于 2019-12-03 10:26:31
After reading a related question about using slices in maps, I became curious about equality in Go. I know it's possible to override the equals method of a Java Object . Is there a similar way to define how Go checks user defined types/structs for equality? If so, there would be a workaround for the issue referenced above. I thought using interface{} values might offer a solution but I received the error message panic: runtime error: hash of unhashable type []int . Go supports equality checking structs. type Person struct { Name string } a := Person{"Bill DeRose"} b := Person{"Bill DeRose"} a

Scala equality with type checking?

烈酒焚心 提交于 2019-12-03 10:11:41
Is there a uniform method to perform equality with type checking? Unfortunately val objectA:String = "test" val objectB:Int = 2 objectA == objectB the equality operator == doesn't complain if objectB is a Int while objectA is a String. I would need an operator like === that perform type checking as well (and I hope it is uniform to all scala obj). Does such operator exist? You need to look at scalaz 's === for type-safe equals - it's implemented as type class there. You can also watch talk by Heiko Seeberger , where he describes how it's implemented: http://days2011.scala-lang.org/node/138/275

How to compare Enums in TypeScript

白昼怎懂夜的黑 提交于 2019-12-03 09:18:53
In TypeScript, I want to compare two variables containing enum values. Here's my minimal code example: enum E { A, B } let e1: E = E.A let e2: E = E.B if (e1 === e2) { console.log("equal") } When compiling with tsc (v 2.0.3) I get the following error: TS2365: Operator '===' cannot be applied to types 'E.A' and 'E.B'. Same with == , !== and != . I tried adding the const keyword but that seems to have no effect. The TypeScript spec says the following: 4.19.3 The <, >, <=, >=, ==, !=, ===, and !== operators These operators require one or both of the operand types to be assignable to the other.

If duck-typing in Python, should you test isinstance?

南笙酒味 提交于 2019-12-03 08:51:13
问题 You have a Python class which needs an equals test. Python should use duck-typing but is it (better/more accurate) to include or exclude an isinstance test in the eq function? For example: class Trout(object): def __init__(self, value): self.value = value def __eq__(self, other): return isinstance(other, Trout) and self.value == other.value 回答1: Using isinstance in __eq__ methods is pretty common. The reason for this is that if the __eq__ method fails, it can fallback on an __eq__ method from

Can I use ' == ' to compare two vectors. I tried it and seems to be working fine. But I don't know whether it will work in more complex situations

不打扰是莪最后的温柔 提交于 2019-12-03 05:25:56
问题 First example: int main(){ using namespace std; vector<int> v1{10, 20, 30, 40, 50}; vector<int> v2{10, 20, 30, 40, 50}; if(v1==v2) cout<<"equal"; else cout<<"unequal"; } // it returns equal Second example: int main(){ using namespace std; vector<int> v1{10, 20, 30, 40, 50}; vector<int> v2{10, 20, 100000, 40, 50}; if(v1==v2) cout<<"equal"; else cout<<"unequal"; } // it returns notequal 回答1: The overload of operator == that works on two std::vectors will compare the vector sizes and return

Difference between hash() and id()

隐身守侯 提交于 2019-12-03 05:03:02
问题 I have two user-defined objects, say a and b . Both these objects have the same hash values. However, the id(a) and id(b) are unequal. Moreover, >>> a is b False >>> a == b True From this observation, can I infer the following? Unequal objects may have the same hash values. Equal objects need to have the same id values. Whenever obj1 is obj2 is called, the id values of both objects is compared, not their hash values. 回答1: There are three concepts to grasp when trying to understand id , hash

Checking equality of interface{}

本小妞迷上赌 提交于 2019-12-03 05:01:34
I am searching a []interface{} slice for a given interface{} value: var v interface{} for i := 0; i < len(A); i++ { if (A[i] == v) { fmt.Println("Gotcha!") break } } In the trivial case the types are int . However what should I do if, for example, the types are some custom struct ? Brian Vanover Thanks to @CodingPickle comment, I provide the following from the Go Programming Language Specification The equality operators == and != apply to operands that are comparable. Regarding interface{} s and structs : Interface values are comparable. Two interface values are equal if they have identical