aochagavia
Idiomatic way to map a result
A common pattern in Elixir is to return {:ok, something} or {:error, something_else} when a function might fail. Sometimes, however, the caller might need to transform the result. Consider for example of a function to divide numbers, which fails when the divisor is 0, and which we want to use as part of a bigger calculation (e.g. divide x by y, then multiply by 42):
case divide(x, y) do
{:ok, result} -> {:ok, result * 42}
{:error, _} = error -> error
end
Given all we want is to transform the result in the :ok case, it feels a bit noisy to require a whole case expression here. Some languages provide a map or map_ok function that helps in these situations, which would simplify things down to the following:
divide(x, y)
|> map_ok(fn result -> result * 42 end)
Does Elixir provide something like this? Or would I need to implement my own helper functions? I know it is easy to implement, but it would be cleaner to be rely on the standard library for this.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 9 of 9 Posts
hauleth
There is
withco struct:aochagavia
Thanks for the reply! The
withconstruct can help quite a bit in the case I mentioned above, but it also becomes hairy when you want to map the value associated to the:erroras well. Having a function such asmap_errorhelps a lot in such a case. There are many more combinator functions possible, which are extensively used in other programming languages with functional roots (Haskell and Rust come to mind). That is why I thought we might have them in Elixir as well (otherwise I guess I might contribute with a library ;))hauleth
The thing with these languages is that these are strongly typed, which mean that you can implement
fmaporbimapfunctions in a way that preserve structure. In Elixir you would need to have function for each of the “types” separately. So as it would be hard to do in Elixir stdlib it is left up to the user to implement them on their own (which is fairly easy, especially when you havewithconstruct).aochagavia
Hmm… My impression is that using
{:ok, something}and{:error, something_else}are so widespread, that you could easily treat them as “Elixir’s implementation of Rust’s Result and Haskell’s Either type”. But then again, I am fairly new to Elixir, so maybe there are more things to consider than I can see right now.Eiji
As far as I know
Elixirdoes not implements any helper function to work with pipes except Kernel.tap/2 and Kernel.then/2, but those works for all type of input - not only specific oneHow about writing a simple macro based on
then?Using this you can work only on data you are interested and optionally if something fails you can handle all errors in one place.
aochagavia
Thanks for the suggestion. A somewhat unrelated question: why use a macro instead of a function for
ok_then?hauleth
There is no strict requirement that
:okor:errorwill be:There is plenty of examples where that do not apply:
GenServer.start_link/3can also return:ignoreCode.fetch_docs/1returns:docs_v1-tuple or:errortupleIO.binread/2can returniodata()on success, unary:error-tuple on error or:eofExamples like that can be written and written, especially when you add Erlang libraries to the mix. It is hard to have “one catch them all” solution.
I would like to know as well, as functions fits much better there.
al2o3cr
If you’re mapping the value in the error side as well, use the
caseyou started with.Eiji
Honestly? I don’t remember.
I know that I read about it somewhere. I wrote the code after looking at
then/2macro code. I guess it was something about compile-time optimization like the macro is translated tocaseat compile time. I tried to find a guide describing it.