equality

Two .NET objects that are equal don't say they are

假如想象 提交于 2019-11-27 22:56:12
I have the following code: object val1 = 1; object val2 = 1; bool result1 = (val1 == val2);//Equals false bool result2 = val1.Equals(val2); //Equals true What's up with that? Is the only way to fix this to go with .Equals() method? The operator == is static, not virtual, so the behaviour is determined by the static type and not the runtime type. The default implementation for == on objects of reference type is to compare the references (although types can implement a different behaviour, for example string ). You have two different objects and they don't have the same reference so == returns

Is there an API method that compares contents of a Seq irrespective of order?

混江龙づ霸主 提交于 2019-11-27 21:11:06
Assuming: val l1 = List(1,2,3) val l2 = List(2,3,1) I want a method that confirms that l1 is equal to l2 (as in same contents but different order). Is there an API method on List/Seq to do this? l1.sameElements(l2) does not work as it verifies order as well. I've come up with the following: l1.foldLeft(l1.size == l2.size)(_ && l2.contains(_)) Is there anything more succinct than the above to do this comparison? If what you want is "these lists contain the same elements, irrespective of order or repetitions": l1.toSet == l2.toSet If what you want is "these lists contain the same elements, and

floating point equality in Python and in general

谁说我不能喝 提交于 2019-11-27 19:50:22
I have a piece of code that behaves differently depending on whether I go through a dictionary to get conversion factors or whether I use them directly. The following piece of code will print 1.0 == 1.0 -> False But if you replace factors[units_from] with 10.0 and factors[units_to ] with 1.0 / 2.54 it will print 1.0 == 1.0 -> True #!/usr/bin/env python base = 'cm' factors = { 'cm' : 1.0, 'mm' : 10.0, 'm' : 0.01, 'km' : 1.0e-5, 'in' : 1.0 / 2.54, 'ft' : 1.0 / 2.54 / 12.0, 'yd' : 1.0 / 2.54 / 12.0 / 3.0, 'mile' : 1.0 / 2.54 / 12.0 / 5280, 'lightyear' : 1.0 / 2.54 / 12.0 / 5280 / 5.87849981e12, }

In Objective-C, what is the equivalent of Java's “instanceof” keyword?

做~自己de王妃 提交于 2019-11-27 19:45:05
问题 I would like to check whether an object (e.g. someObject ) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType ). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type of an object is equal to a another type. In Java, I can write: someObject.getClass().equals(SpecifiedType.class) How can this be done in Objective-C? 回答1: Try [myObject class] for returning the class of an object. You can make exact comparisons

How to detect array equality in JavaScript?

纵然是瞬间 提交于 2019-11-27 18:01:36
问题 There are two arrays in JavaScript, they are both in the following format: [{'drink':['alcohol', 'soft', 'hot']}, {'fruit':['apple', 'pear']}]; I need to detect if the two arrays are equal or not. they are considered equal if they contain the same elements in a different order. How can I make that? 回答1: Check the length of both arrays Loop through the first array, compare each variable to the second array. If 1 and 2 are both the same, your array is equal. Function to compare objects/arrays:

Equality of functions in Haskell

守給你的承諾、 提交于 2019-11-27 17:40:32
问题 I am trying to define a function which would take a Double -> Double function and return its mathematical derivative. I have tried doing the following: der :: (Double -> Double) -> (Double -> Double) der f | f == exp = exp | otherwise = undefined but Haskell does not support == on Double -> Double values. Is what I am trying to do impossible in Haskell? 回答1: Yes, what you are trying to do is impossible in Haskell, and in general: deciding whether two functions are equal for all possible

Difference between String#equals and String#contentEquals methods

老子叫甜甜 提交于 2019-11-27 17:33:24
What is the difference between the String#equals method and the String#contentEquals method? The String#equals() not only compares the String's contents, but also checks if the other object is also an instance of a String . The String#contentEquals() only compares the contents (the character sequence) and does not check if the other object is also an instance of String . It can be anything as long as it is an implementation of CharSequence which covers a.o. String , StringBuilder , StringBuffer , CharBuffer , etc. To put it easily: String.contentEquals() is the smarter brother of String.equals

How default .equals and .hashCode will work for my classes?

此生再无相见时 提交于 2019-11-27 17:28:41
Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are the default implementations? From Object class? And what are they? How the default equals will work? How the default hashCode will work and what will return? == will just check if they reference to the same object, so it's easy, but what about equals() and hashCode() methods? Etienne de Martel Yes, the default implementation is Object's (generally speaking; if you inherit from a class that redefined

Equality of truthy and falsy values (JavaScript)

回眸只為那壹抹淺笑 提交于 2019-11-27 16:29:19
I've encountered something which seems inconsistent on the part of the interpreter, though I know it probably makes sense and I just don't understand it. It has to do with evaluating the equality of a truthy/falsy values and Booleans. Example 1: if (document.getElementById('header')) { //run code here } If the element with an id of 'header' is found in the document, the condition is true because the presence of an object is considered truthy. Example 2: if (document.getElementById('header) == true) { // run code here } Presuppose the referenced element is found within the document. It was

Bug with equals operator and NSObjects in Swift 2.0?

只谈情不闲聊 提交于 2019-11-27 16:18:48
问题 Ok, something strange is happening when writing your own equals operator for NSObject subclasses in Swift 2.0 like this: func ==(lhs: MyObject, rhs: MyObject) -> Bool { return lhs.identifier == rhs.identifier } For a class that looks like this: class MyObject: NSObject { let identifier: String init(identifier: String) { self.identifier = identifier } } This used to work just fine in Swift 1.2 and below. It still kind of works: let myObject1 = MyObject(identifier: "A") let myObject2 = MyObject