Skip to content

How to extract data from Result type #1985

Discussion options

You must be logged in to vote

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 functions map, then, etc.

[[1], [1, 1]]
|> list.last
|> result.map(fn(x) {
  // do something with x here...
})

This can also be written with use

use x <- result.map(list.last([[1], [1, 1]]))
// do something with x here...

Another way is to use result.unwrap or a case expression to extract the value out and use a default value if it is an Error.

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@suraj-mandal
Comment options

Answer selected by suraj-mandal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants