From: Martin J. <mar...@em...> - 2005-06-09 20:03:06
|
On Mon, 6 Jun 2005, Nicolas Cannasse wrote: > > > I would say it's bad API design to use a "merge" function in order to > remove > > > elements. > > > A more specific function should be provided. > > > > I do not think so. When your merge gives accidentally 0, and the value > > should be removed, you do not want to scan the map once more to remove > > the zero ... > > Then it's no longer a merge... > What you want is a more generic function that will do a merge+filter. "reduce" as suggested by Florian seems to be appropriate. The following signatures seem good to me: (* Exception Remove can be raised by the user-given functions and means "do not keep a binding to this key" for both union and inter. *) val union : ('key -> 'a -> 'c) -> ('key -> 'a -> 'b -> 'c) -> ('key -> 'b -> 'c) -> ('key, 'a) t -> ('key, 'b) t -> ('key, 'c) t val inter : ('key -> 'a -> 'b -> 'c) -> ('key, 'a) t -> ('key, 'b) t -> ('key, 'c) t val diff : ('key, 'a) t -> ('key, 'b) t -> ('key, 'a) t Plus maybe some specialized versions for efficiency and/or convenience. I am not sure if the gain in performance would be terrific, and specialized versions of union and inter can easily be defined out of PMap anyway, so I wouldn't insist too much on having those functions or not. Martin -- Martin Jambon, PhD http://martin.jambon.free.fr |