Skip to content

Instantly share code, notes, and snippets.

@moonpolysoft
Created April 17, 2010 01:16
Show Gist options
  • Select an option

  • Save moonpolysoft/369156 to your computer and use it in GitHub Desktop.

Select an option

Save moonpolysoft/369156 to your computer and use it in GitHub Desktop.
object ComparableSeq {
implicit def seqToComparableSeq[A <: Comparable[A]](seq : Seq[A]) : ComparableSeq[A] = {
new ComparableSeq(seq)
}
}
class ComparableSeq[A <: Comparable[A]](seq : Seq[A]) extends Seq[A] with Ordered[Seq[A]] {
def compare(lhs : Seq[A]) : Int = {
if (seq.length > lhs.length) {
1
} else if (seq.length < lhs.length) {
-1
} else {
compareRecurse(seq.toList, lhs.toList)
}
}
private def compareRecurse(rhs : List[A], lhs : List[A]) : Int = (rhs,lhs) match {
case (Nil, Nil) => 0
case (rHead :: rTail, lHead :: lTail) =>
rHead compareTo lHead match {
case 0 => compareRecurse(rTail,lTail)
case i => i
}
}
}
inferred type arguments [Byte] do not conform to method seqToComparableSeq's type parameter bounds [A <: java.lang.Comparable[A]]
[error] case add : Ipv4Address => seqToComparableSeq(this.bytes).compare(add.bytes)
[error] ^
[error] one error found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment