How to extract data from Result type #1985
-
How can I extract data from the Result datatype. I mean for example: import gleam/list.{last}
last_pascal_output = [[1], [1, 1]]
let last_row: Result(List(Int), Nil) = last(last_pascal_output) This should return
Here how to extract |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello! There's a few different ways to do it. The main way is to not extract the value, but instead to push the rest of the computation into the result. The [[1], [1, 1]]
|> list.last
|> result.map(fn(x) {
// do something with x here...
}) This can also be written with use x <- result.map(list.last([[1], [1, 1]]))
// do something with x here... Another way is to use Lastly you can use |
Beta Was this translation helpful? Give feedback.
Hello! There's a few different ways to do it.
The main way is to not extract the value, but instead to push the rest of the computation into the result. The
result
module is useful for this with it's functionsmap
,then
, etc.This can also be written with
use
Another way is to use
result.unwrap
or a case expression to extract the value out and use a default value if it is anError
.Lastly you can use
assert Ok(x) = list.last(my_list)
. This is convenient but great care must be used as it will cause the program to crash if the patt…