First post here, so forgive me if this question has an obvious answer!
I was looking at the Js.Option
and the Belt.Option
in the API documentation and was wondering what are the differences between the two. The Js.Option
seems to be “data-last” in the sense that the option you pass in comes last, as compared to Belt.Option
“data-first” approach.
The data first seems nicer to use when chaining together operations eg:
// This is a bit cleaner...
Some(thing)->flatMap(doSomething)->flatMap(doSomethingElse)
// Than this
Some(thing)->andThen(doSomething, _)->andThen(doSomethingElse, _)
Other than data-first arguably looking a bit nicer, are there any other differences that are important (practically speaking?)
Example
Here is a little example with the compiled JS for illustration:
let reciprocal = (. x) => x == 0. ? None : Some(1. /. x)
let jsVersion = Js.Option.andThen(reciprocal, Some(2.))
let beltVersion = Belt.Option.flatMapU(Some(2.), reciprocal)
Here is the compiled JS output:
function reciprocal(x) {
if (x === 0) {
return ;
} else {
return 1 / x;
}
}
var jsVersion = Js_option.andThen(reciprocal, 2);
var beltVersion = Belt_Option.flatMapU(2, reciprocal);
So the JS functions are different, but will it really make a difference which to use? Should one be preferred over the other?
TL;DR
Are there any important differences between Js.Option.andThen
and Belt.Option.flatMapU
(uncurried version) other than the order of their arguments?