Add some Belt.Option utility functions

I’ve been getting started with ReScript and am missing some common functions available in the F# Option module. Little things - just a few lines of code that I had to write myself and put in my own “OptionUtil” module. For example, the orElse function is useful to avoid nesting when you want to chain a few parsers together until the first one returns a value. combine is useful to reduce an array of options. There are a handful of useful other ones that would be handy if they were baked in. What is the process for making these kinds of improvements?

let orElseWith = (o, f) =>
  switch o {
  | Some(_) => o
  | None => f()
  }

let orElse = (o, v) =>
  switch o {
  | Some(_) => o
  | None => Some(v)
  }

let combine = (f, a, b) =>
  switch (a, b) {
  | (None, b) => b
  | (_, None) => a
  | (Some(a), Some(b)) => Some(f(a, b))
  }

let toArray = o =>
  switch o {
  | None => []
  | Some(x) => [x]
  }
7 Likes

I’d recommend you to create a PR in rescript-compiler repo to add those functions to Belt.Option

1 Like

I looked into this. I can’t build ReScript on Windows or at least the docs sounded too complicated. If someone else wants to open the PR I could research the functions that are needed and provide the implementation (in the new ReScript syntax) and documentation for them.