equality

Sets, Functors and Eq confusion

家住魔仙堡 提交于 2019-11-28 03:38:57
A discussion came up at work recently about Sets, which in Scala support the zip method and how this can lead to bugs, e.g. scala> val words = Set("one", "two", "three") scala> words zip (words map (_.length)) res1: Set[(java.lang.String, Int)] = Set((one,3), (two,5)) I think it's pretty clear that Set s shouldn't support a zip operation, since the elements are not ordered. However, it was suggested that the problem is that Set isn't really a functor, and shouldn't have a map method. Certainly, you can get yourself into trouble by mapping over a set. Switching to Haskell now, data AlwaysEqual

Comparing two structs using ==

亡梦爱人 提交于 2019-11-28 02:24:12
问题 I am trying to compare two structs using equals (==) in C#. My struct is below: public struct CisSettings : IEquatable<CisSettings> { public int Gain { get; private set; } public int Offset { get; private set; } public int Bright { get; private set; } public int Contrast { get; private set; } public CisSettings(int gain, int offset, int bright, int contrast) : this() { Gain = gain; Offset = offset; Bright = bright; Contrast = contrast; } public bool Equals(CisSettings other) { return Equals

How may I test the equivalency of enumeration cases with associated values in Swift 4

痴心易碎 提交于 2019-11-28 02:00:32
I would like to test the equivalency of a couple variables of enumeration types, like this: enum AnEnumeration { case aSimpleCase case anotherSimpleCase case aMoreComplexCase(String) } let a1 = AnEnumeration.aSimpleCase let b1 = AnEnumeration.aSimpleCase a1 == b1 // Should be true. let a2 = AnEnumeration.aSimpleCase let b2 = AnEnumeration.anotherSimpleCase a2 == b2 // Should be false. let a3 = AnEnumeration.aMoreComplexCase("Hello") let b3 = AnEnumeration.aMoreComplexCase("Hello") a3 == b3 // Should be true. let a4 = AnEnumeration.aMoreComplexCase("Hello") let b4 = AnEnumeration

CLR JIT optimizations violates causality?

馋奶兔 提交于 2019-11-28 01:49:23
I was writing an instructive example for a colleague to show him why testing floats for equality is often a bad idea. The example I went with was adding .1 ten times, and comparing against 1.0 (the one I was shown in my introductory numerical class). I was surprised to find that the two results were equal ( code + output ). float @float = 0.0f; for(int @int = 0; @int < 10; @int += 1) { @float += 0.1f; } Console.WriteLine(@float == 1.0f); Some investigation showed that this result could not be relied upon (much like float equality). The one I found most surprising was that adding code after the

Set “in” operator: uses equality or identity?

若如初见. 提交于 2019-11-28 01:47:15
class A(object): def __cmp__(self): print '__cmp__' return object.__cmp__(self) def __eq__(self, rhs): print '__eq__' return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2]) Why does first line prints True, but second prints False? And neither enters operator eq ? I am using Python 2.6 You need to define __hash__ too. For example class A(object): def __hash__(self): print '__hash__' return 42 def __cmp__(self, other): print '__cmp__' return object.__cmp__(self, other) def __eq__(self, rhs): print '__eq__' return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2

Which equality test does Ruby's Hash use when comparing keys?

余生长醉 提交于 2019-11-28 00:51:42
问题 I have a wrapper class around some objects that I want to use as keys in a Hash. The wrapped and unwrapper objects should map to the same key. A simple example will be this: class A attr_reader :x def initialize(inner) @inner=inner end def x; @inner.x; end def ==(other) @inner.x==other.x end end a = A.new(o) #o is just any object that allows o.x b = A.new(o) h = {a=>5} p h[a] #5 p h[b] #nil, should be 5 p h[o] #nil, should be 5 I've tried ==, ===, eq? and hash all to no avail. 回答1: Hash uses

how equal operator works with primitive and object type data

前提是你 提交于 2019-11-27 23:41:30
问题 I know its a very basic question but I want to be clear about the concept. I want to know how == operator works in case of primitive and object type. For example Integer a = 1; int b = 1; System.out.println(a == b) how a is compared with b , whereas a contain the ref of object that contains value 1. Can somebody clearify it to me how it works internally? 回答1: In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables

What's the fastest way to compare two arrays for equality?

爷,独闯天下 提交于 2019-11-27 23:40:58
问题 I have two arrays of objects which are likely to have the same values, but in a different order, e.g. { "cat", "dog", "mouse", "pangolin" } { "dog", "pangolin", "cat", "mouse" } I wish to treat these two arrays as equal. What's the fastest way to test this? 回答1: I can't guarantee that this is the fastest , but it's certainly quite efficient: bool areEquivalent = array1.Length == array2.Length && new HashSet<string>(array1).SetEquals(array2); EDIT: SaeedAlg and Sandris raise valid points about

Why is [] !== [] in JavaScript? [duplicate]

 ̄綄美尐妖づ 提交于 2019-11-27 23:14:51
This question already has an answer here: Why isn't [1,2,3] equal to itself in Javascript? 6 answers Why is [] !== [] in JavaScript? I read through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness but I could not find anything that explains this. Edit: I don't think this question or this question is an exact duplicate of mine. It asks about the == operator which just behaves crazy. The answer is an answer to my question but it's not the same question. That does a reference check on the two array literals to see if they are the same instance. The fact

nhibernate: what are the best practices for implementing equality?

故事扮演 提交于 2019-11-27 23:11:18
I think that Entities should implement equality by primary key comparison as default, but the nhibernate documentation recommends using business identity: The most obvious way is to implement Equals()/GetHashCode() by comparing the identifier value of both objects. If the value is the same, both must be the same database row, they are therefore equal (if both are added to an ISet, we will only have one element in the ISet). Unfortunately, we can't use that approach. NHibernate will only assign identifier values to objects that are persistent, a newly created instance will not have any