Following are a few tricks that have been very useful while using Elixir…

The first is one of the most helpful ones. update_in and put_in are macros that allow you to update nested structures with just a given path and a function, case you want to transform the current value, or a new value:

data = %{
  nested: %{
    value: 7
  }
}

put_in(data.nested.value, 13)
# %{nested: %{value: 13}}

update_in(data.nested.value, fn old -> old * 3 end)
# %{nested: %{value: 21}}

new_key = :hi
put_in(data.nested[new_key], 23)
# %{nested: %{hi: 23, value: 7}}

 

Using with, instead of having several cases inside each other, you can pattern match multiple return values. It returns either the result of the do block, if everything happened as expected, or the first faulty return value. It is very useful when creating multiple resources, like chaining calls to GenServer.start_link:

v1 = {:ok, 3}
v2 = {:ok, 5}

with {:ok, a} <- v1,
     {:ok, b} <- v2,
     do: {:ok, a + b}
# {:ok, 8}


v2 = {:error, 5}

with {:ok, a} <- v1,
     {:ok, b} <- v2,
     do: {:ok, a + b}
# {:error, 5}

 

While looking at Cauldron’s source code, I learned how you can use pattern matching to get the last part of a string:

"/a/b/" <> something = "/a/b/hello"
something == "hello"
# true

 

When reviewing the Getting Started section, I remembered that you can check if an element belongs to a collection with:

1 in [1, 2, 3]
{:a, 1} in %{ a: 1, b: 2, c: 3 }

I don’t use this last very often, because usually pattern matching provides a better/cleaner solution.