|
|
|
|
|
by tel
651 days ago
|
|
In Haskell, that's usually that's done using `do` syntax. do
a <- somePartialResult
b <- partialFunction1 a
c <- partialFunction2 a b
return c
where we assume signatures like somePartialResult : Either<A, Error>
partialFunction1 : A -> Either<B, Error>
partialFunction2 : A -> B -> Either<C, Error>
this overall computation has a signature Either<C, Error>. The way it works is that the first failing computation (the first Either that's actually Left-y) will short-circuit and become the final result value. Only if all of the partial computations succeed (are Right-y) will the final result by Right(c).In Haskell we don't have an early return syntax like `return` and function scope. Instead, we construct something equivalent using `do` syntax. This can be a little weightier than `return`, but the upside is that you can construct other variants of things like early returns that can be more flexible. |
|
Or, should we resort to a method of `Either` that transforms its `Left` in that case?