Skip to content

Instantly share code, notes, and snippets.

View darcros's full-sized avatar

Dario Crosa darcros

  • Milan, Italy
  • 06:29 (UTC +02:00)
View GitHub Profile
@darcros
darcros / frequency_server.erl
Created July 14, 2020 11:50
Concurrent Programming in Erlang - The University of Kent - chapter 4.5
-module(frequency_server).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2]).
-export([start/0, stop/0, allocate/0, deallocate/1]).
get_frequencies() -> [10,11,12,13,14,15].
%% gen_sever implementation
init(_Args) ->
@darcros
darcros / frequency.erl
Created July 12, 2020 17:26
Concurrent Programming in Erlang - The University of Kent - chapter 3.13
-module(frequency).
-export([init/1]).
-export([start_freq/1, stop_freq/1, allocate_freq/1, deallocate_freq/2]).
-define(TIMEOUT, 5000).
%% server implementation
allocate({[], Allocated}, _Pid) ->
@darcros
darcros / frequency_hardened.erl
Created July 3, 2020 12:49
Concurrent Programming in Erlang - The University of Kent - chapter 2.12
-module(frequency_hardened).
-export([init/0, start/0, stop/0, allocate/0, deallocate/1]).
-define(TIMEOUT, 5000).
%% server implementation
allocate({[], Allocated}, _Pid) ->
{{[], Allocated}, {error, no_frequency}};
@darcros
darcros / frequency_updated.erl
Created June 30, 2020 18:48
Concurrent Programming in Erlang - The University of Kent - chapter 2.5
-module(frequency_updated).
-export([loop/1, init/1, stop/0, allocate_freq/0, deallocate_freq/1, demo/0, use_freq/0]).
-define(TIMEOUT, 5000).
%% server implementation
allocate({[], Allocated}, _Pid) ->
{{[], Allocated}, {error, no_frequency}};
@darcros
darcros / frequency.erl
Created June 29, 2020 16:00
Concurrent Programming in Erlang - The University of Kent - chapter 2.3
-module(frequency).
-export([init/0, loop/1]).
allocate({[], Allocated}, _Pid) ->
{{[], Allocated}, {error, no_frequency}};
allocate({[Freq | Free], Allocated}, Pid) ->
case lists:keymember(Pid, 2, Allocated) of
true -> {{[Freq | Free], Allocated}, {error, already_allocated}};
@darcros
darcros / mailbox.erl
Last active June 27, 2020 19:53
Concurrent Programming in Erlang - The University of Kent - chapter 1.8
-module(mailbox).
-export([handle_unordered/0, handle_ordered/1, run/0]).
%% Handle messages in the order that they arrive
handle_unordered() ->
receive
stop ->
ok;
{_, Message} ->
@darcros
darcros / palindrome.erl
Last active July 29, 2020 04:15
Concurrent Programming in Erlang - The University of Kent - chapter 1.5
-module(palindrome).
-export([server/0, client/2, start_load_balancer/1, test_all/0]).
rem_punct(String) ->
lists:filter(fun (Ch) ->
not lists:member(Ch, "\"'\t\n ")
end,
String).