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(...) ...

Get Web Development with ReasonML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.