Skip to content

Instantly share code, notes, and snippets.

@pcewing
Last active June 18, 2016 05:03
Show Gist options
  • Select an option

  • Save pcewing/aaab4cc534eb70c2b6b581ab9e99e35e to your computer and use it in GitHub Desktop.

Select an option

Save pcewing/aaab4cc534eb70c2b6b581ab9e99e35e to your computer and use it in GitHub Desktop.
Formatting a function definition with long pattern matching/guard clauses.
# A message is supposed to come in like this but we don't know if the fields are filled out.
message = %{
route: %{
destination: "Tim",
source: "Paul"
},
type: :friend_request,
payload: %{
size: 32,
content: %{
message: "I'd like to be your friend."
}
}
}
# Use pattern matching/guard clauses to verify it at least has a target and a message.
def handle_friend_request (
%{route: %{destination: target}, type: :friend_request, payload: %{content: %{message: message}}}
) when is_binary(target) and is_binary(message) do
# Write some code here.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment