I’m usually a fan of map and flatMap when using Option, but removing the runtime hit for calling functions is very appealing.
Here’s I function I found in one of my projects.
let query =
ctx.url.search
->String.replace("?", "")
->String.split("&")
->Array.map(String.split(_, "="))
->Array.find(x => x[0] == Some("query"))
->Option.flatMap(Array.at(_, 1))
->Option.map(decodeURIComponent)
->Option.getOr("")
Which I think would look like this:
let query = {
let? Some(query) = ctx.url.search
->String.replace("?", "")
->String.split("&")
->Array.map(String.split(_, "="))
->Array.find(x => x[0] == Some("query"))
let? Some(uriComponent) = query->Array.at(1)
let? Some(decoded) = decodeUriComponent(uriComponent)
decoded->Option.getOr("")
}
I’m not a fan of the extra names for things and I would probably just use Some(x) all the time. This would remove any ReScript specific functions from the output (excluding the getOr), which would be really nice.
Having a way to easily bail to a fallback value like Option.getOr would be nice to have. I would have to have that be a switch right now to get rid of that function.
I’m not sure anything I am doing get seriously impacted enough by the performance for me to move away from the pipe and pointfree style I like, but it is nice to have this available.