equality

Sets, Functors and Eq confusion

对着背影说爱祢 提交于 2019-12-17 17:30:04
问题 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.

nhibernate: what are the best practices for implementing equality?

纵饮孤独 提交于 2019-12-17 16:30:57
问题 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

floating point equality in Python and in general

拥有回忆 提交于 2019-12-17 15:44:51
问题 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 /

Python testing whether a string is one of a certain set of values

柔情痞子 提交于 2019-12-17 14:53:13
问题 I'm learning python on codecademy and my current task is this: Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use s for string). The shut_down function should return "Shutting down..." when it gets "Yes" , "yes" , or "YES" as an argument, and "Shutdown aborted!" when it gets "No" , "no" , or "NO" . If it gets anything other than those inputs, the function should return "Sorry, I didn't understand you." Seemed easy to me but somehow I

When is the `==` operator not equivalent to the `is` operator? (Python)

[亡魂溺海] 提交于 2019-12-17 12:57:49
问题 I noticed I can use the == operator to compare all the native data types (integers, strings, booleans, floating point numbers etc) and also lists, tuples, sets and dictionaries which contain native data types. In these cases the == operator checks if two objects are equal. But in some other cases (trying to compare instances of classes I created) the == operator just checks if the two variables reference the same object (so in these cases the == operator is equivalent to the is operator) My

Can I define custom operator overloads in Javascript? [duplicate]

孤街浪徒 提交于 2019-12-17 10:58:45
问题 This question already has answers here : Javascript: operator overloading (5 answers) Overloading Arithmetic Operators in JavaScript? (11 answers) Closed 3 years ago . Is it possible to define custom operators between instances of a type in JavaScript? For example, given that I have a custom vector class, is it possible to use vect1 == vect2 to check for equality, whilst the underlying code would be something like this? operator ==(a, b) { return a.x == b.x && a.y == b.y && a.z == b.z; }

JavaScript - === vs == operators performance

萝らか妹 提交于 2019-12-17 10:44:29
问题 A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C . It was said that there is no difference in the performance between < and <= as they are interpreted as same/similar machine commands. At the same time, in our company's "best practices", it was said that we should always use "===" to compare things instead of "==". So, I started to wonder if this is always appropriate as I am used to using the "==" and "typeof ... == " and do not want to change my

Why does 1234 == '1234 test' evaluate to true? [duplicate]

那年仲夏 提交于 2019-12-17 08:54:22
问题 This question already exists : Closed 7 years ago . Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test') 回答1: Because you are using the == (similarity) operator and PHP is coercing the string to an int. To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered

Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?

梦想的初衷 提交于 2019-12-17 07:24:40
问题 As far as I understand, Scala's == defines the natural equality of two objects. I expected that Array(0,1,2) == Array(0,1,2) compares the natural equality. For example, checks if all elements of the array return true when compared with the corresponding elements of the other array. People told me that Scala's Array is just a Java [] which only compares identity. Wouldn't it be more meaningful to override Array 's equals method to compare natural equality instead? 回答1: Scala 2.7 tried to add

What's the difference between eq, eql, equal and equalp, in Common Lisp?

前提是你 提交于 2019-12-17 04:12:58
问题 What's the difference between eq , eql , equal and equalp , in Common Lisp? I understand that some of them check types, some of them check across types an all that, but which is which? When is one better to use than the others? 回答1: From Common Lisp: Equality Predicates (eq x y) is true if and only if x and y are the same identical object. The eql predicate is true if its arguments are eq , or if they are numbers of the same type with the same value, or if they are character objects that