Last active
August 17, 2017 15:41
-
-
Save bresson/fb7203289087d1775a9ecb4114c7cbf1 to your computer and use it in GitHub Desktop.
Erlang frequency server enhancements frequency.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% frequency server enhancements using list comprehension | |
% My first instinct was to solve this problem through recursion. However, I decided to use list comprehension / filter just to sample a new flavor. With list comprehension, I was able to pattern match but the process felt more like checking the length of the Frequencies/allocated list to determine if the request was valid.% If anyone can provide a hint, I had to create two functions, is_allocated and is_deallocated, the first to check if the Pid is already allocated a frequency and the second to check if a requested deallocate frequency was in fact allocated. The functions are very similar and should be one function. However, I couldn't figure out how to pass in a parameter that will inform the following line to check Client == Y or Client == X | |
% [X || {X, Y} <- Allocated, Client == Y]. | |
init() -> | |
Frequencies = {get_frequencies(), []}, | |
loop(Frequencies). | |
% Hard Coded | |
get_frequencies() -> [10,11,12,13,14,15]. | |
loop(Frequencies) -> | |
receive | |
{request, Pid, allocate} -> | |
case is_allocated(Pid, Frequencies) of | |
% empty, Pid not already allocated | |
[] -> {NewFrequencies, Reply} = allocate(Frequencies, Pid); | |
[X|_Xs] -> {NewFrequencies, Reply} = {Frequencies, {'Already allocated', X}} | |
end, | |
% io:format('free frequencies ~p~n', [NewFrequencies]), | |
Pid ! {reply, Reply}, | |
loop(NewFrequencies); | |
{request, Pid , {deallocate, Freq}} -> | |
case is_deallocated2(Freq, Pid, Frequencies) of | |
% empty allocation, do not deallocate | |
[] -> {NewFrequencies, Reply} = {Frequencies, {'Nothing allocated', Pid}}; | |
[X|_Xs] -> {NewFrequencies, Reply} = deallocate(Frequencies, Freq) | |
end, | |
% io:format('deallocated, free frequencies ~p~n', [NewFrequencies]), | |
Pid ! {reply, Reply}, | |
loop(NewFrequencies); | |
{request, Pid, stop} -> | |
Pid ! {reply, stopped} | |
end. | |
allocate({[], Allocated}, _Pid) -> | |
{{[], Allocated}, {error, no_frequency}}; | |
allocate( {[Freq|Free], Allocated}, Pid) -> | |
{{Free, [{Freq, Pid}|Allocated]}, {ok, Freq}}. | |
deallocate({Free, Allocated}, Freq) -> | |
NewAllocated=lists:keydelete(Freq, 1, Allocated), | |
{ {[Freq|Free], NewAllocated}, {ok, "Deallocated"}}. | |
is_allocated(Client, {Free, Allocated}) -> | |
[X || {X, Y} <- Allocated, Client == Y]. | |
% check for frequency && Pid | |
is_deallocated2(Freq, Pid, {Free, Allocated}) -> | |
[X || {X, Y} <- Allocated, Freq == X, Pid == Y]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment