Martin Flower - 8 months ago 78

Scala Question

I am new to the Scala language - I seem to have a problem finding the right syntax for sorting and comparing two Option[Seq[String]]. The two should be equals irrespective of contents order. The last line in this test fails. I hope this code sufficiently documents the question.

`package seqtest`

import org.scalatest.{Matchers, FlatSpec}

class SortCollectionsTest extends FlatSpec with Matchers {

"Different seq order" should "not affect equality" in {

val seqAB1 = Seq("A","B")

val seqAB2 = Seq("A","B")

// two different sequences are equal

assert(seqAB1 == seqAB2)

val seqBA1 = Seq("B","A")

// two different sequences are not equal if different order

assert(seqAB1 != seqBA1)

// but is possible to convert sequence to list and sort

assert(seqAB1.toList.sorted == seqBA1.toList.sorted)

// now do the same thing with Option

val someSeqAB1 = Some(Seq("A","B"))

val someSeqAB2 = Some(Seq("A","B"))

// two different option sequences are equal

assert(someSeqAB1 == someSeqAB2)

val someSeqBA = Some(Seq("B","A"))

// two different optional sequences are not equal if different order

assert(someSeqAB1 != someSeqBA)

// Option can be converted into list (unsorted)

assert(someSeqAB1.toList != someSeqBA.toList)

// problem

// two different optional sequences cannot be sorted

// compilation error

// Error:(42, 30) No implicit Ordering defined for Seq[String].

// Error:(42, 30) not enough arguments for method sorted: (implicit ord: scala.math.Ordering[Seq[String]])List[Seq[String]].

// Unspecified value parameter ord.

assert(someSeqAB1.toList.sorted == someSeqBA.toList.sorted)

}

}

Answer

I think the much easier solution would be to map the options:

```
assert(someSeqAB1.map{ _.sorted } == someSeqBA.map{ _.sorted })
```

Btw, to the sorted version the assertion is equal.