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

Request: Base.Option.value_merge #116

Closed
mroch opened this issue Sep 16, 2021 · 3 comments
Closed

Request: Base.Option.value_merge #116

mroch opened this issue Sep 16, 2021 · 3 comments
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@mroch
Copy link

mroch commented Sep 16, 2021

As with Option.map vs Option.value_map, it'd be useful (albeit rarely!) to have a value_merge function:

let value_merge a b ~default ~f =
  match a, b with
  | None, None -> default
  | None, x | x, None -> x
  | Some a, Some b -> Some (f a b)

I know, I know, I can do Option.merge a b ~f |> Option.value ~default but wanted to throw it out there.

@bcc32
Copy link
Member

bcc32 commented Sep 16, 2021

Thanks for the suggestion, @mroch. Do you have a use case in mind for where this function might come up commonly?

@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Sep 17, 2021
@benjub
Copy link
Contributor

benjub commented Sep 22, 2021

@mroch: regardless of its usefulness, I think your proposed function should be:

let value_merge a b ~default ~f =
  match a, b with
  | None, None -> default
  | None, Some x | Some x, None -> x
  | Some a, Some b -> f a b

(which indeed corresponds to fun a b ~default ~f -> Option.merge a b ~f |> Option.value ~default).

@tdelvecchio-jsc
Copy link

I have not heard of much need for a function like this, and as it's easy enough to just write Option.merge a b ~f |> Option.value ~default, we would rather not clutter Base with an extra function. I'm closing this issue for now, feel free to re-open it if you have a strong argument for adding it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

5 participants