Working with Belt.Option
There’s nothing wrong with the preceding example—it works as advertised and accounts for possible invalid data. But all of those switches make the code harder to read. The Belt.Option module gives us functions that let us write this sort of code without all the busy work. The Belt.Option module is part of Belt standard library shipped with BuckleScript.
In line 4, we want the value of an option to go to a function that takes an ordinary value and returns an option. In this case, we use the Belt.Option.flatMap function. This function expects two parameters: an option(..) value, and the name of a function that takes a normal value and returns an option(...).
Belt.Option.flatMap extracts the value of the option(...) ...