Elixir Tips

Elixir Tips

  • 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"}
    
    47 upvotes