I want to add some helpful implicits to both mutable and immutable TreeMaps and TreeSets in Scala.
Here is my attempt:
First try to define the least upper bound of TreeMap and TreeSet that has
headOption
/lastOption
(fromGenTraversableLike
) andfrom
/to
/until
(fromSorted
):type SortedCollection[A, Repr <: SortedCollection[A, Repr]] = collection.generic.Sorted[A, Repr] with collection.GenTraversableLike[A, Repr]
Write my util:
implicit class RichSortedCollection[A, Repr <: SortedCollection[A, Repr]](s: SortedCollection[A, Repr]) { def greaterThanOrEqualTo(a: A): Option[A] = s.from(a).headOption def lessThan(a: A): Option[A] = s.until(a).lastOption def lessThanOrEqualTo(a: A): Option[A] = s.to(a).lastOption }
This only works partially: SortedSet#greaterThan
compiles but TreeMap#greaterThan
does not. How do I fix it?