danj

danj

Conditional pipe operator on error tuple

I wanted to share this code for a problem that regularly comes up and that I usually solve with multi-headed functions or with clauses. The issue: handling an error tuple in a pipeline situation by skipping steps.

Here’s the code: (Important: There’s a bug in this macro, see below)

  defmacro left ~> right do
    quote do
      case unquote( left ) do
        { :error, _ } = err -> err
        val -> unquote( Macro.pipe( left, right, 0 ) )
      end
    end
  end

Which makes possible a pipeline that looks like:

start_value
|> step_a
~> step_b
~> step_c
|> step that handles  { :error, _ } or a real result

Any thoughts on this approach?

Dan

Most Liked

mudasobwa

mudasobwa

Creator of Cure

While exceptional library is great, this partucular issue is fully covered by Elixir core Kernel.SpecialForms.with/1.

OvermindDL1

OvermindDL1

That’s pretty common, what what the exceptional library on hex.pm does (it handles raw values, tagged ok/error tuples, returning exceptions, multiple styles of handling errors, etc…). I use it quite a lot.

danj

danj

This is the Kernel.|>/2 macro sequence. It felt a little unsatisfying knowing it translates into Macro.pipe. With a little more time and testing it seems to cleanly reduce to:

unquote( Macro.pipe( (quote do: val), right, 0 ) )

A simple test looking like:

iex(101)> 14+7 ~> IO.inspect(label: 1) |> IO.inspect( label: 2) ~> IO.inspect( label: 3 ) |> IO.inspect( label: 4 )
case(14 + 7 ~> IO.inspect(label: 1) |> IO.inspect(label: 2)) do
  {:error, _} = err ->
    err
  val ->
    IO.inspect(val, label: 3)
end
case(14 + 7) do
  {:error, _} = err ->
    err
  val ->
    IO.inspect(val, label: 1)
end
1: 21
2: 21
3: 21
4: 21
21

The code printing comes from an inline Macro.to_string( ast ) for illustration. The code shows backward since the unpipe rolls the pipe up in reverse while nesting.

This is the error tuple scenario:

iex(102)> { :error, 14+7 } ~> IO.inspect(label: 1) |> IO.inspect( label: 2) ~> IO.inspect( label: 3 ) |> IO.inspect( label: 4 )
case({:error, 14 + 7} ~> IO.inspect(label: 1) |> IO.inspect(label: 2)) do
  {:error, _} = err ->
    err
  val ->
    IO.inspect(val, label: 3)
end
case({:error, 14 + 7}) do
  {:error, _} = err ->
    err
  val ->
    IO.inspect(val, label: 1)
end
2: {:error, 21}
4: {:error, 21}
{:error, 21}

Where Next?

Popular in Discussions Top

und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement