Confusing minAfter and maxBefore

Hi all,
I recently stumbled over some (imho) confusing behavior of the methods minAfter and maxBefore in TreeSet. Let me explain what I mean with an example:

import scala.collection.immutable.TreeSet

val t = TreeSet(0)
println(t.minAfter(0))       // evaluates to Some(0)
println(t.maxBefore(0))   // evaluates to None

Since the names are so similar I would have expected a more coherent behavior. So minAfter is equal to “ceilEntry” whereas maxBefore is equal to “lowerEntry”. I would suggest an extension of the current api to the methods: lowerEntry, floorEntry, higherEntry and ceilEntry. I think the new names would be more descriptive and not clash with the old names which could be set to deprecated.

Best Tirregs

according to the docs

  • minAfter: Find the smallest element larger than or equal to a given key.
  • maxBefore: Find the largest element less than a given key.

I get what you’re saying tho: would be cool if the or equal to applied in both instances

I wonder if there is a theoretical explanation for the difference

In general, libraries usually find that they want this sort of asymmetry: otherwise, you wind up either missing the element in the middle, or double-counting it, by accident.

1 Like

I think without the asymmetry it would be more cumbersome to e.g. implement the methods I mentioned above, which is rather rather trivial with the given methods. At the end of the day I do not have an argument other than: I would prefer it this way because I think it is clearer. Somehow I just expected to get the same behavior for minAfter if I called maxBefore with reversed ordering.