aochagavia

aochagavia

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.

Showing Posts 1 to 9

hauleth

hauleth

There is with co struct:

with {:ok, v} <- foo(), do: {:ok, v * 2}
aochagavia

aochagavia OP

Thanks for the reply! The with construct 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 :error as well. Having a function such as map_error helps 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

hauleth

The thing with these languages is that these are strongly typed, which mean that you can implement fmap or bimap functions 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 have with construct).

aochagavia

aochagavia OP

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

Eiji

As far as I know Elixir does 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 one

How about writing a simple macro based on then?

defmodule Example do
  defmacro ok_then(value, fun) do
    quote do
      case unquote(value) do
        {:ok, result} -> {:ok, unquote(fun).(result)}
        result -> result
      end
    end
  end

  def divide(_x, 0), do: {:error, :zero_divisor}
  def divide(x, y), do: {:ok, div(x, y)}
end

require Example

x
|> Example.divide(y)
|> Example.ok_then(&(&1 * 6))
|> Example.ok_then(&(&1 * 7))
|> then(fn
  {:ok, result} -> IO.inspect(result, label: "Transformed result")
  {:error, :zero_divisor} -> IO.warn("Zero divisor problem …")
  {:error, error} -> IO.warn("Unexpected error #{error}")
end)

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

aochagavia OP

Thanks for the suggestion. A somewhat unrelated question: why use a macro instead of a function for ok_then?

hauleth

hauleth

There is no strict requirement that :ok or :error will be:

  • tuple
  • with exactly 2 elements

There is plenty of examples where that do not apply:

  • GenServer.start_link/3 can also return :ignore
  • Code.fetch_docs/1 returns :docs_v1-tuple or :error tuple
  • IO.binread/2 can return iodata() on success, unary :error-tuple on error or :eof

Examples 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

al2o3cr

If you’re mapping the value in the error side as well, use the case you started with.

Eiji

Eiji

Honestly? I don’t remember. :sweat_smile:

I know that I read about it somewhere. I wrote the code after looking at then/2 macro code. I guess it was something about compile-time optimization like the macro is translated to case at compile time. I tried to find a guide describing it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews