Skip to content

Instantly share code, notes, and snippets.

@aeden
Last active December 10, 2015 19:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aeden/4483378 to your computer and use it in GitHub Desktop.
Save aeden/4483378 to your computer and use it in GitHub Desktop.
$ erl
Erlang R15B (erts-5.9) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9 (abort with ^G)
1> c(example1).
{ok,example1}
2> example1:sum(10, 20).
30
1> c(example1_1).
{ok,example1_1}
2> M = example1_1:multiplier(2).
#Fun<example1_1.0.86284448>
3> lists:map(M, [1,2,3,4]).
[2,4,6,8]
4> lists:map(example1_1:multiplier(10), [1,2,3,4]).
[10,20,30,40]
1> X = 10.
10
2> X = 20.
** exception error: no match of right hand side value 20
1> c(example2).
{ok,example2}
2> example2:sum([10, 20, 30, 40, 50, 100]).
250
1> c(example3).
{ok,example3}
2> example3:babble(100).
[<0.38.0>,<0.39.0>,<0.40.0>,<0.41.0>,<0.42.0>,<0.43.0>,
<0.44.0>,<0.45.0>,<0.46.0>,<0.47.0>,<0.48.0>,<0.49.0>,
<0.50.0>,<0.51.0>,<0.52.0>,<0.53.0>,<0.54.0>,<0.55.0>,
<0.56.0>,<0.57.0>,<0.58.0>,<0.59.0>,<0.60.0>,<0.61.0>,
<0.62.0>,<0.63.0>,<0.64.0>,<0.65.0>,<0.66.0>|...]
The number is 4
The number is 3
The number is 2
The number is 1
The number is 56
The number is 61
The number is 55
1> c(example4).
{ok,example4}
2> Adder = spawn(example4, adder, []).
<0.38.0>
3> Adder ! {self(), add, 10, 30}.
{<0.31.0>,add,10,30}
4> flush().
Shell got 40
ok
1> c(example5).
{ok,example5}
2> gen_server:start_link({local, example5}, example5, [], []).
{ok,<0.38.0>}
3> gen_server:call(example5, "Hello, there").
Received call from {<0.31.0>,#Ref<0.0.0.77>}: "Hello, there"
"Thanks for playing"
4> gen_server:cast(example5, {foo, 5}).
ok
Received cast: {foo,5}
1> c(example6).
{ok,example6}
2> example6:start_link().
{ok,<0.38.0>}
3> example6:sum(1, 2).
3
4> example6:sum([10, 20, 30, 40]).
100
5> example6:async_sum(1, 2).
Sum: 3
ok
1> c(example7).
{ok,example7}
2> c(example7_1).
{ok,example7_1}
3> example7:start_link().
example7_1 start_link() called.
{ok,<0.43.0>}
4> example7_1:do_it().
0
5> example7_1:do_it().
1
6> example7_1:do_it().
2
7> example7_1:do_it().
Terminated with reason: normal
example7_1 start_link() called.
ok
8> example7_1:do_it().
0
9> example7_1:do_it().
1
-module(functional).
-export([sum/2]).
sum(A, B) ->
A + B.
-module(example1_1).
-export([multiplier/1]).
multiplier(X) ->
fun(A) ->
A * X
end.
-module(example2).
-export([sum/1]).
sum(Values) ->
sum(Values, 0).
sum([], Total) -> Total;
sum([Value|Rest], Total) ->
sum(Rest, Total + Value).
-module(example3).
-export([babble/1]).
count(N) ->
timer:sleep(10),
io:format("The number is ~p~n", [N]).
babble(NumberOfTimes) ->
[spawn(fun() -> count(N) end) || N <- lists:seq(1, NumberOfTimes)].
-module(example4).
-export([adder/0]).
adder() ->
receive
{From, add, A, B} -> From ! sum(A, B);
{From, _} -> From ! error
end.
sum(A, B) ->
A + B.
-module(example5).
-behavior(gen_server).
% Gen server hooks
-export([init/1, handle_call/3, handle_cast/2]).
-export([handle_info/2, terminate/2, code_change/3]).
-record(state, {}).
% Gen server callbacks
init([]) ->
{ok, #state{}}.
handle_call(Message, From, State) ->
io:format("Received call from ~p: ~p~n", [From, Message]),
{reply, "Thanks for playing", State}.
handle_cast(Message, State) ->
timer:sleep(10),
io:format("Received cast: ~p~n", [Message]),
{noreply, State}.
handle_info(_Message, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_PreviousVersion, State, _Extra) -> {ok, State}.
-module(example6).
-behavior(gen_server).
-export([start_link/0, sum/1, sum/2, async_sum/2]).
% Gen server hooks
-export([init/1, handle_call/3, handle_cast/2]).
-export([handle_info/2, terminate/2, code_change/3]).
-record(state, {}).
% Public API
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
sum(A, B) ->
gen_server:call(?MODULE, {sum, A, B}).
sum(Values) ->
gen_server:call(?MODULE, {sum, Values}).
async_sum(A, B) ->
gen_server:cast(?MODULE, {sum, A, B}).
% Gen server callbacks
init([]) ->
{ok, #state{}}.
handle_call({sum, A, B}, _, State) ->
{reply, sum_list([A, B]), State};
handle_call({sum, Values}, _, State) ->
{reply, sum_list(Values), State}.
handle_cast({sum, A, B}, State) ->
io:format("Sum: ~p~n", [sum_list([A, B])]),
{noreply, State}.
handle_info(_Message, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_PreviousVersion, State, _Extra) -> {ok, State}.
% Internal functions
sum_list(Values) ->
lists:foldl(fun(X, Sum) -> X + Sum end, 0, Values).
-module(example7).
-behavior(supervisor).
-export([start_link/0]).
% Supervisor hooks
-export([init/1]).
% Public API
start_link() ->
supervisor:start_link(?MODULE, []).
% Supervisor callbacks
init([]) ->
{ok, {{one_for_one, 10, 10}, [{example7_1, {example7_1, start_link, []}, permanent, 1000, worker, [example7_1]}]}}.
-module(example7_1).
-behavior(gen_server).
-export([start_link/0, do_it/0]).
% Gen server hooks
-export([init/1, handle_call/3, handle_cast/2]).
-export([handle_info/2, terminate/2, code_change/3]).
-record(state, {count, max_iterations=3}).
% Public API
start_link() ->
io:format("~p start_link() called.~n", [?MODULE]),
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
do_it() ->
gen_server:call(?MODULE, doit).
% Gen server callbacks
init([]) ->
{ok, #state{count=0}}.
handle_call(doit, _, State) ->
MaxIterations = State#state.max_iterations,
case State#state.count of
MaxIterations -> {stop, normal, ok, State};
_ ->{reply, State#state.count, State#state{count=State#state.count + 1}}
end.
handle_cast(doit, State) ->
{noreply, State#state{count=State#state.count + 1}}.
handle_info(Message, State) ->
io:format("Received info message: ~p~n", [Message]),
{noreply, State}.
terminate(Reason, _State) ->
io:format("Terminated with reason: ~p~n", [Reason]),
ok.
code_change(_PreviousVersion, State, _Extra) -> {ok, State}.
-module(example8).
-behavior(application).
-export([start/2, stop/1]).
start(Type, Args) ->
example7_1:start_link().
stop(State) ->
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment