Skip to content

Instantly share code, notes, and snippets.

@dergraf
Created October 23, 2012 10:23
Show Gist options
  • Save dergraf/3938055 to your computer and use it in GitHub Desktop.
Save dergraf/3938055 to your computer and use it in GitHub Desktop.
-module(dbis_sample).
-export([i_am_a_function/1, pattern/1]).
%% this is a comment
i_am_a_function(Param) ->
Number = 10,
Atom = atom,
String = "string",
EmptyList = [],
List = [Param, Number, Atom, String, EmptyList],
Tuple = {Param, Number, Atom, String, List},
AnonymousFunction = fun(SomeParam) ->
io:format("Lets print the param to the console ~p~n", [SomeParam])
end,
%& calling the anonymous function
AnonymousFunction(Tuple),
%% create a new process
Pid = spawn(fun() ->
receive
{Sender, Msg} ->
AnonymousFunction(pattern(Msg)),
Sender ! done;
_ ->
invalid_format
end
end),
erlang:send_after(1000, Pid, {self(), "hello world"}),
receive
done ->
ok;
_ ->
error(invalid_message)
after
2000 ->
error(timeout)
end.
%% Pattern matching example
pattern(A) when is_number(A) ->
A * A;
pattern(A) when is_list(A) ->
lists:reverse(A);
pattern(A) when is_function(A) ->
A();
pattern(A) ->
A.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment