Today I Learned

hashrocket A Hashrocket project

Pattern match anonymous function clauses in elixir

Did you know that you can use pattern matching headers with anonymous functions in elixir? It works like this:

anon = fn
  {:ok, value} -> "Got value: #{value}"
  _ -> "Doing something else"
end

# IO.puts anon.({:ok, 42})   # => "Got value: 42"
# IO.puts anon.(:ok)   # => "Doing something else"
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.