Skip to main content

Alexander Koutmos

akoutmos

akoutmos

The string concat operator

You may know that the <> operator is used to concat binaries (strings)...but did you also know you can use it for pattern matching binaries?
iex> "You can" <> " " <> "concat binaries!"
#=> "You can concat binaries!"

iex> case "user:b4c52a55-e2d9-446f-908d-42c9812f2e8a" do
      "admin:" <> id -> {:admin, id}
      "user:" <> id -> {:user, id}
      _ -> {:error, :invalid_format}
    end
{:user, "b4c52a55-e2d9-446f-908d-42c9812f2e8a"}
46 upvotes

© 2021 Zest Creative, LLC