Skip to content

Instantly share code, notes, and snippets.

@MasseGuillaume
Created September 30, 2014 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasseGuillaume/b23a744a9aa2b77fe1b6 to your computer and use it in GitHub Desktop.
Save MasseGuillaume/b23a744a9aa2b77fe1b6 to your computer and use it in GitHub Desktop.
node ordering
sealed trait Node[T]
case class Leaf[T](v: T) extends Node[T]
case class Branch[T](l: Node[T], r: Node[T]) extends Node[T]
implicit def nodeOrdering[W, C](implicit num: Numeric[W]): Ordering[Node[(W, C)]] = {
def weight(a: Node[(W, C)]): Tuple2[W, C] = a match {
case Leaf((w, c)) => (w, c)
case Branch(l, r) => {
val (wl, cl) = weight(l)
val (wr, _) = weight(r)
(num.plus(wl, wr), cl)
}
}
Ordering.by(weight)
// No implicit Ordering defined for (W, C).
// not enough arguments for method by: (implicit ord: scala.math.Ordering[(W, C)])scala.math.Ordering[Node[(W, C)]]. Unspecified value parameter ord.
}
@MasseGuillaume
Copy link
Author

implicit def nodeOrdering[W: Ordering, C: Ordering](implicit num: Numeric[W]): Ordering[Node[(W, C)]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment