Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005740OCamlOCaml standard librarypublic2012-08-27 04:302013-01-04 08:34
Reporterfuruse 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
StatusfeedbackResolutionopen 
PlatformOSOS Version
Product Version4.00.0 
Target VersionFixed in Version 
Summary0005740: Set should have a way to access the "middle" element and its left and right nodes.
DescriptionIt 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.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0008520)
xleroy (administrator)
2012-11-17 18:07

Hi Jun,

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?
(0008541)
thelema (reporter)
2012-11-21 21:59

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.
(0008689)
berenger (reporter)
2013-01-04 08:34

Don't you need a find actually?

Cf. http://caml.inria.fr/mantis/view.php?id=5864 [^]

- Issue History
Date Modified Username Field Change
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
Powered by Mantis Bugtracker