Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add_new to map #2991

Closed
vicuna opened this issue Oct 13, 2001 · 3 comments
Closed

Suggestion: add_new to map #2991

vicuna opened this issue Oct 13, 2001 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Oct 13, 2001

Original bug ID: 576
Reporter: administrator
Assigned to: @alainfrisch
Status: resolved (set by @alainfrisch on 2016-12-08T10:57:53Z)
Resolution: suspended
Priority: normal
Severity: feature
Category: standard library
Tags: patch, junior_job
Related to: #2748
Monitored by: @thizanne @gasche

Bug description

same definition as Map.add, except that it will throw an exception if the
key already exists. (I think that this should have been the default
behaviour of add, and the current add should have been called replace)

let rec add x data = function
    Empty ->
      Node(Empty, x, data, Empty, 1)
  | Node(l, v, d, r, h) as t ->
      let c = Ord.compare x v in
      if c = 0 then
        raise XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      else if c < 0 then
        bal (add x data l) v d r
      else
        bal l v d (add x data r)

/mattias

File attachments

@vicuna
Copy link
Author

vicuna commented Nov 13, 2002

Comment author: administrator

Reasonable suggestion. Find good name?

@vicuna
Copy link
Author

vicuna commented Jun 19, 2014

Comment author: @thizanne

Please ignore Map.add_new.patch (no test). Map.add_new.2.patch solves this (and there is a test).

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @alainfrisch

I agree this would be a reasonable addition. Since we are moving away from using exception, the function should return an option (None if the map already contained the element). A similar function should also be added to Set (and to Labeled versions). Marking as suspended, but a PR is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants