如何比较两个 Traversable 包含完全相同的元素?
How to compare that two Traversables contain exactly the same elements?
我想检查两个 Traversable
是否包含相同的元素而不考虑它们的顺序。
所以,我自己试了一下,写了下面这个简单的例子:
implicit val l = 3
implicit def equality(implicit l: Int) = new Equality[String] {
override def areEqual(a: String, b: Any): Boolean = (a, b) match {
case (_, b: Int) => a.toInt == b
}
}
"Test" should "check how equality works" in {
List("1") should contain theSameElementsAs Vector(1) //Fine
List("1", "2") should contain theSameElementsAs Vector(1, 2) //Fine
List("1", "2", "3") should contain theSameElementsAs Vector(1, 2, 3) //Fine
List("1", "2", "2") should contain theSameElementsAs Vector(1, 2, 2) //Error
List("2", "1") should contain theSameElementsAs Vector(1, 2) //Error
}
正如 the documentation 所说:
The "contain theSameElementsAs
" syntax lets you assert that two
aggregations contain the same objects
它不应该考虑重复和顺序。这有什么问题吗?
在左侧使用 Int
列表而不是字符串列表。
"Test" should "check how equality works" in {
List(1) should contain theSameElementsAs Vector(1) //Fine
List(1, 2) should contain theSameElementsAs Vector(1, 2) //Fine
List(1, 2, 3) should contain theSameElementsAs Vector(1, 2, 3) //Fine
List(1, 2, 2) should contain theSameElementsAs Vector(1, 2, 2) //Fine
List(2, 1) should contain theSameElementsAs Vector(1, 2) //Fine
}
输出:
[info] Test
[info] - should check how equality works
[info] Run completed in 354 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
我认为根本问题是 equality
关系并不像人们期望的那样具有反射性。即如果 x == y
那么 y == x
。
Scalacheck 尝试使用提供的 equality
函数在目标集合中 count the number of repetitions。
当遇到equality(2,2)
、tryEquality
、fails internally with a ClassCastException
时,默认比较为false。
底线:Equality
需要反思(正如古老的数学所规定的那样)才能获得预期的结果。
我想检查两个 Traversable
是否包含相同的元素而不考虑它们的顺序。
所以,我自己试了一下,写了下面这个简单的例子:
implicit val l = 3
implicit def equality(implicit l: Int) = new Equality[String] {
override def areEqual(a: String, b: Any): Boolean = (a, b) match {
case (_, b: Int) => a.toInt == b
}
}
"Test" should "check how equality works" in {
List("1") should contain theSameElementsAs Vector(1) //Fine
List("1", "2") should contain theSameElementsAs Vector(1, 2) //Fine
List("1", "2", "3") should contain theSameElementsAs Vector(1, 2, 3) //Fine
List("1", "2", "2") should contain theSameElementsAs Vector(1, 2, 2) //Error
List("2", "1") should contain theSameElementsAs Vector(1, 2) //Error
}
正如 the documentation 所说:
The "
contain theSameElementsAs
" syntax lets you assert that two aggregations contain the same objects
它不应该考虑重复和顺序。这有什么问题吗?
在左侧使用 Int
列表而不是字符串列表。
"Test" should "check how equality works" in {
List(1) should contain theSameElementsAs Vector(1) //Fine
List(1, 2) should contain theSameElementsAs Vector(1, 2) //Fine
List(1, 2, 3) should contain theSameElementsAs Vector(1, 2, 3) //Fine
List(1, 2, 2) should contain theSameElementsAs Vector(1, 2, 2) //Fine
List(2, 1) should contain theSameElementsAs Vector(1, 2) //Fine
}
输出:
[info] Test
[info] - should check how equality works
[info] Run completed in 354 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
我认为根本问题是 equality
关系并不像人们期望的那样具有反射性。即如果 x == y
那么 y == x
。
Scalacheck 尝试使用提供的 equality
函数在目标集合中 count the number of repetitions。
当遇到equality(2,2)
、tryEquality
、fails internally with a ClassCastException
时,默认比较为false。
底线:Equality
需要反思(正如古老的数学所规定的那样)才能获得预期的结果。