Given getPartition is called per row, this is a pretty expensive pattern matching. (If you look at the byte code generated, each case statement gets turned into a branch & function call that is hard to be inlined). It is best to do this matching in the constructor and assign an anon function to run the binary search.