#how to result.unwrap and list.map together

1 messages · Page 1 of 1 (latest)

hexed oyster
#

Hello, this pattern is showing up frequently and I'm wondering if this is a more idiomatic way to write this:

      let list_result = foo() // returns Result(List(String), SomeError)
      case list_result {
         Ok(my_list) -> list.map(my_list, string.length)
          _ -> []
       }

Basically, error case should be unwrapped to [] and good case needs to be mapped to some other list. Is there a way to achieve this in fewer lines of code.

tardy copper
#
wibble()
|> result.unwrap([])
|> list.map(string.length)

You almost never should do this though as you're throwing away the error

#

It's more common and generally better to do do this

#
wibble()
|> result.map(list.map(_, string.length))