You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 5740 Reporter: furuse Assigned to:@alainfrisch Status: resolved (set by @alainfrisch on 2016-12-09T08:22:29Z) Resolution: won't fix Priority: normal Severity: feature Version: 4.00.0 Category: standard library
Bug description
It would be handy if Set provides an easy access to its binary tree structure:
val middle (* or whatever *) : t -> (t * elt * t) option
let middle = function
| Empty -> None
| Node (left, v, right, _) -> Some (left, v, right)
Currently we can have this function with very dirty workaround using iter/filter and side effects. Such a implementation by me recently broke by the change of search order of filter.
This function exposes the internal detail of Set, therefore it is against the spirit of the implementation encapsulation, but still it is very useful.
The text was updated successfully, but these errors were encountered:
This function is not just "against the spirit of the implementation encapsulation": it is not even a function, in that it can map equal sets (according to the "equal" predicate) to different results.
So, pray tell, what is a good use case for this function? Something that you can't achieve just by using the current Set interface, esp. Set.fold?
Batteries has a Set.choose value that just returns an arbitrary element of a non-empty set, which I find useful for initializing folds where there's no reasonable initial value to start the fold with (and just a pain to be constantly matching None) or recursive functions that repeatedly choose an element of the set and do some work that possibly removes other elements from the set before repeating.
This pattern of removing an arbitrary element of a set became common enough that I added a Set.pop : t -> elt * t that returns an arbitrary element and a new set of the remaining elements.
That said, neither of these breach the encapsulation, as long as you don't depend on any properties of the chosen element.
Original bug ID: 5740
Reporter: furuse
Assigned to: @alainfrisch
Status: resolved (set by @alainfrisch on 2016-12-09T08:22:29Z)
Resolution: won't fix
Priority: normal
Severity: feature
Version: 4.00.0
Category: standard library
Bug description
It would be handy if Set provides an easy access to its binary tree structure:
val middle (* or whatever *) : t -> (t * elt * t) option
let middle = function
| Empty -> None
| Node (left, v, right, _) -> Some (left, v, right)
Currently we can have this function with very dirty workaround using iter/filter and side effects. Such a implementation by me recently broke by the change of search order of filter.
This function exposes the internal detail of Set, therefore it is against the spirit of the implementation encapsulation, but still it is very useful.
The text was updated successfully, but these errors were encountered: