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[19] 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(...) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access