|Anonymous | Login | Signup for a new account||2014-03-11 15:37 CET|
|Main | My View | View Issues | Change Log | Roadmap|
|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0005740||OCaml||OCaml standard library||public||2012-08-27 04:30||2013-01-04 08:34|
|Target Version||Fixed in Version|
|Summary||0005740: Set should have a way to access the "middle" element and its left and right nodes.|
|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.
|Tags||No tags attached.|
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.
Don't you need a find actually?
Cf. http://caml.inria.fr/mantis/view.php?id=5864 [^]
|2012-08-27 04:30||furuse||New Issue|
|2012-11-15 19:43||doligez||Status||new => acknowledged|
|2012-11-17 18:07||xleroy||Note Added: 0008520|
|2012-11-17 18:07||xleroy||Status||acknowledged => feedback|
|2012-11-21 21:59||thelema||Note Added: 0008541|
|2013-01-04 08:34||berenger||Note Added: 0008689|
|Copyright © 2000 - 2011 MantisBT Group|