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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.