Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could try <code>.toSeq</code> for ordered collections and <code>.toSet</code> for unordered, which captures what you want as far as I understand it. </p> <p>The following passes:</p> <pre><code>class Temp extends FunSuite with ShouldMatchers { test("1") { Array(1, 2).toSeq should equal (List(1, 2).toSeq) } test("2") { Array(2, 1).toSeq should not equal (List(1, 2).toSeq) } test("2b") { Array(2, 1) should not equal (List(1, 2)) } test("3") { Iterable(2, 1).toSet should equal (Iterable(1, 2).toSet) } test("4") { Iterable(2, 1) should not equal (Iterable(1, 2)) } } </code></pre> <p>BTW a <code>Set</code> is not ordered.</p> <p><strong>edit:</strong> To avoid removing duplicate elements, try <code>toSeq.sorted</code>. The following pass:</p> <pre><code> test("5") { Iterable(2, 1).toSeq.sorted should equal (Iterable(1, 2).toSeq.sorted) } test("6") { Iterable(2, 1).toSeq should not equal (Iterable(1, 2).toSeq) } </code></pre> <p><strong>edit 2:</strong> For unordered collections where elements cannot be sorted, you can use this method:</p> <pre><code> def sameAs[A](c: Traversable[A], d: Traversable[A]): Boolean = if (c.isEmpty) d.isEmpty else { val (e, f) = d span (c.head !=) if (f.isEmpty) false else sameAs(c.tail, e ++ f.tail) } </code></pre> <p>e.g. (note use of symbols <code>'a 'b 'c</code> which have no defined ordering)</p> <pre><code> test("7") { assert( sameAs(Iterable(2, 1), Iterable(1, 2) )) } test("8") { assert( sameAs(Array('a, 'c, 'b), List('c, 'a, 'b) )) } test("9") { assert( sameAs("cba", Set('a', 'b', 'c') )) } </code></pre> <hr> <p>Alternative <code>sameAs</code> implementation: </p> <pre><code> def sameAs[A](c: Traversable[A], d: Traversable[A]) = { def counts(e: Traversable[A]) = e groupBy identity mapValues (_.size) counts(c) == counts(d) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload