3 ms·
I am not sure about Elexir, but in Ocaml, someone using this style would gravitate towards Result.map and Result.bind (Defined like this: given (Ok a) or (Err b
by nephanth 3y ago
I am not sure about Elexir, but in Ocaml, someone using this style would gravitate towards Result.map and Result.bind
(Defined like this: given (Ok a) or (Err b),
- (Result.map f) returns (Ok (f(a))) or (Err b)
- (Result.bind f) returns f(a) or (Err b)
)
One could then write
params.id
|> find_user
|> Result.bind (verify_user_has_access_to params.post_id)
|> etc
Reasons for that are 2-fold
1. It de-clutters your functions (you do not need that match statement anymore)
2. It becomes evident
- which functions will simply pass down errors (bind or map) vs. which ones may handle them
- which functions may raise new errors (bind can, map can't)