equality

Find whether two Swift Arrays contain the same elements

浪尽此生 提交于 2019-12-18 09:45:25
问题 I have two arrays let a1 = [obj1, obj2, obj3] let a2 = [obj3, obj2, obj1] Assume that the array elements are custom objects which are not sortable . I just want to check if the Array s contain the same elements, in any order. I tried this: if a1==a2 { print("S1 is the same as S2") } else { print("S1 is not the same as S2") } but I get "S1 is not the same as S2" as output. All I could think of are two solutions Sort and compare (doesn't work if elements are not sortable, say complex numbers)

Should I use a concatenation of my string fields as a hash code?

旧时模样 提交于 2019-12-18 04:35:19
问题 I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } } I'm implementing equality and so I need to override the hash code. At first I was going to use the hashcode formula from EJ but then I thought: These are all string fields, can't I just just use

When would == be overridden in a different way to .equals?

萝らか妹 提交于 2019-12-18 03:59:15
问题 I understand the difference between == and .equals. There are plenty of other questions on here that explain the difference in detail e.g. this one: What is the difference between .Equals and == this one: Bitwise equality amongst many others. My question is: why have them both (I realise there must be a very good reason) - they both appear to do the same thing (unless overridden differently). When would == be overloaded in a different way to how .equals is overridden? 回答1: My question is: why

What is the best way to check two List<T> lists for equality in C#

你。 提交于 2019-12-18 03:55:31
问题 There are many ways to do this but I feel like I've missed a function or something. Obviously List == List will use Object.Equals() and return false . If every element of the list is equal and present in the same location in the opposite list then I would consider them to be equal. I'm using value types, but a correctly implemented Data object should work in the same fashion (i.e I'm not looking for a shallow copied list, only that the value of each object within is the same). I've tried

Checking for NaN presence in a container

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:25:33
问题 NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.] I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart? Is it safe to check for NaN presence

Checking for NaN presence in a container

*爱你&永不变心* 提交于 2019-12-18 03:23:06
问题 NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.] I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart? Is it safe to check for NaN presence

3 Equals or Case Equality operator

狂风中的少年 提交于 2019-12-17 19:31:46
问题 In Ruby Integer === 5 returns true . Similarly String === "karthik" returns true . However, 5 === Integer returns false . And "karthik" === String . Why is the operator not commutative? 回答1: The simple answer is: because it doesn't make sense. The relationship the operator describes is not commutative, why should the operator be? Just look at your own examples: 5 is an Integer . But is Integer a 5 ? What does that even mean ? === is the case subsumption operator , and subsumption doesn't

Why String.Equals is returning false?

故事扮演 提交于 2019-12-17 19:27:12
问题 I have the following C# code (from a library I'm using) that tries to find a certificate comparing the thumbprint. Notice that in the following code both mycert.Thumbprint and certificateThumbprint are strings. var certificateThumbprint = AppSettings.CertificateThumbprint; var cert = myStore.Certificates.OfType<X509Certificate2>().FirstOrDefault( mycert => mycert.Thumbprint != null && mycert.Thumbprint.Equals(certificateThumbprint) ); This fails to find the certificate with the thumbprint

Pointer equality in Haskell?

我是研究僧i 提交于 2019-12-17 19:21:18
问题 Is there any notion of pointer quality in Haskell? == requires things to be deriving Eq, and I have something which contains a (Value -> IO Value), and neither -> nor IO derive Eq. EDIT: I'm creating an interpreter for another language which does have pointer equality, so I'm trying to model this behavior while still being able to use Haskell functions to model closures. EDIT: Example: I want a function special that would do this: > let x a = a * 2 > let y = x > special x y True > let z a = a

Swift: Overriding == in subclass results invocation of == in superclass only

五迷三道 提交于 2019-12-17 17:55:16
问题 I've got a class A , which conforms to Equatable protocol and implements == function. In subclass B I override == with more checks. However, when I do comparison between two arrays of instances of B (which both have type Array<A> ), == for A is invoked. Of course if I change type of both arrays to Array<B> , == for B is invoked. I came up with the following solution: A.swift: internal func ==(lhs: A, rhs: A) -> Bool { if lhs is B && rhs is B { return lhs as! B == rhs as! B } return ... }