Description
The current merge_neighbors is distance-based, which makes it useful only for the narrow use case of dealing with "sliver" intervals, essentially by merging them into adjacent, longer intervals. A more useful general case would be to perform a merge of adjacent intervals based on some condition in the adjacent intervals' data objects. For instance, consider a case where the data objects are dictionaries. Under a conditional merge_neighbors, one would only perform the merge of adjacent intervals where the item values for a given key in the respective data objects of two adjacent intervals are equal to each other, or to some specified value. This type of conditional merge is independent of interval length, and is dependent rather on the data content of adjacent intervals.
This type of conditional merge can currently be performed, but only with rather cumbersome and clunky code. It is necessary to iterate over the sorted intervals of a tree, comparing the data object of the current interval to that the previous interval, and, if the condition is met, deleting them, and then constructing a new interval that covers the span of the adjacent deleted intervals, building a new data object for the new "merged' interval based on the remains of the deleted intervals' data objects, and then inserting the new "merged" interval back into the tree. It's tedious and not particularly performant. Adding the ability to specify a merge condition into the arguments for merge_neighbors would be very helpful.