Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created July 28, 2012 18:23
Show Gist options
  • Save robertsosinski/3194279 to your computer and use it in GitHub Desktop.
Save robertsosinski/3194279 to your computer and use it in GitHub Desktop.
Erlang Pattern Matching and Function Guarding
-module(parser).
-export([parse/1]).
% Example 1:
%
% Tuple1 = {person, [
% {name, [{first, "Robert"}, {last, "Sosinski"}]},
% {home, [{city, "Hoboken"}, {state, "New Jersey"}]},
% {info, [{age, 30}, {hobbies, [saxophone, beer, french]}]}
% ]}.
%
% First: Robert, Last: Sosinski, Full: [{first,"Robert"},{last,"Sosinski"}]
% City: Home of Sinatra, State: Home of The Sopranos
% Is an adult with hobbies
% Example 2:
%
% Tuple2 = {person, [
% {name, [{first, "Johnny"}]},
% {home, [{city, "New York City"}, {state, "New York"}]},
% {info, [{age, 16}, {hobbies, [drums, cola, japanese]}]}
% ]}.
%
% First: Johnny
% City: New York City, State: New York
% Is a minor with hobbies
% Example 3:
%
% Tuple3 = {person, [
% {name, [{last, "Doe"}]},
% {home, [{city, "San Francisco"}, {state, "California"}]},
% {info, [{age, 28}, {hobbies, []}]}
% ]}.
%
% Last: Doe
% City: San Francisco, State: California
% Is an adult without hobbies
% Example 4:
%
% Tuple4 = {object, [
% {batch, 1234}, {lot, 9876}
% ]}.
%
% Object parsing is currently not implemented...
% Example 5:
%
% Tuple5 = {food, [
% {name, quiche}, {meal, breakfast}
% ]}.
%
% Invalid structure, cannot parse...
%% API Functions
parse({person, Person}) -> parse_person(Person);
parse({object, Object}) -> parse_object(Object);
parse(_) -> io:format("Invalid structure, cannot parse...~n").
%% Internal Functions
parse_person([{name, Name}]) ->
parse_person_name(Name);
parse_person([{name, Name}, {home, Home}]) ->
parse_person_name(Name),
parse_person_home(Home);
parse_person([{home, Home}, {info, Info}]) ->
parse_person_home(Home),
parse_person_info(Info);
parse_person([{name, Name}, {info, Info}]) ->
parse_person_name(Name),
parse_person_info(Info);
parse_person([{name, Name}, {home, Home}, {info, Info}]) ->
parse_person_name(Name),
parse_person_home(Home),
parse_person_info(Info).
parse_person_name([{first, First}]) ->
io:format("First: ~s~n", [First]);
parse_person_name([{last, Last}]) ->
io:format("Last: ~s~n", [Last]);
parse_person_name(Full = [{first, First}, {last, Last}]) ->
io:format("First: ~s, Last: ~s, Full: ~p~n", [First, Last, Full]).
parse_person_home([{city, "Hoboken"}, {state, "New Jersey"}]) ->
io:format("City: Home of Sinatra, State: Home of The Sopranos~n");
parse_person_home([{city, City}, {state, State}]) ->
io:format("City: ~s, State: ~s~n", [City, State]).
parse_person_info([{age, Age}, {hobbies, Hobbies}]) when Age >= 18, length(Hobbies) > 0 ->
io:format("Is an adult with hobbies~n");
parse_person_info([{age, _Age}, {hobbies, Hobbies}]) when length(Hobbies) > 0 ->
io:format("Is a minor with hobbies~n");
parse_person_info([{age, Age}, {hobbies, _Hobbies}]) when Age >= 18 ->
io:format("Is an adult without hobbies~n");
parse_person_info([{age, _Age}, {hobbies, _Hobbies}]) ->
io:format("Is a minor without hobbies~n").
parse_object(_Input) -> io:format("Object parsing is currently not implemented...~n").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment