Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Bind in ReScript (not to import JS, but to bind multiple Option or Result)

$
0
0

Rescript does not have a similar specialized syntax for these type of (i.e. monadic) computations.

There are some tools to help:

  • If we’re processing a singular piece of data through multiple steps, -> can prevent the calls from nesting.
let result = 
  Some(5)
  ->Option.filter(n => n != 0)
  ->Option.flatMap(n => 10 / n)
  ->Option.map(n => n + 7)
  • If we need to combine a small number of values, a switch works nicely:
let result = 
  switch (Some(1), Some(2)) {
    | (Some(a), Some(b)) => Some(a + b)
    | (_, _) => None 
  }

There’s not a straightforward approach to building up a scope over time, though.


Viewing all articles
Browse latest Browse all 2592

Trending Articles