-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_nitrogenous_base.erl
65 lines (58 loc) · 1.57 KB
/
count_nitrogenous_base.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-module(count_nitrogenous_base).
-export([start/0, rpc/1, server/0, restarter/0]).
-export([count_nb/5]).
start() ->
spawn(?MODULE, restarter, []).
% Thanks Fred Hébert :)
restarter() ->
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, server, []),
register(serverproc, Pid),
receive
{'EXIT', Pid, normal} ->
ok;
{'EXIT', Pid, shutdown} ->
ok;
{'EXIT', Pid, _} ->
restarter()
end.
server() ->
receive
{From, Ref, {count_atcg, A, T, G, C, String}} ->
From ! {Ref, count_nb(A, T, G, C, String)},
server();
{From, Ref, Other} ->
From ! {Ref, {error, Other}},
server()
end.
rpc(Ask) ->
Ref = make_ref(),
serverproc ! {self(), Ref, Ask},
receive
{Ref, Response} ->
Response
after 2000 ->
timeout
end.
count_nb(_A, _T, _G, _C, []) ->
[_A | [ _T | [ _G | [_C | []]]]];
count_nb(A, T, G, C, [H|Cdr]) when H == $A; H == $a ->
count_nb(A+1, T, G, C, Cdr);
count_nb(A, T, G, C, [H|Cdr]) when H == $T; H == $t ->
count_nb(A, T+1, G, C, Cdr);
count_nb(A, T, G, C, [H|Cdr]) when H == $G; H == $g ->
count_nb(A, T, G+1, C, Cdr);
count_nb(A, T, G, C, [H|Cdr]) when H == $C; H == $c ->
count_nb(A,T,G,C+1,Cdr).
% 1> c(count_nitrogenous_base).
% {ok,count_nitrogenous_base}
% 2> count_nitrogenous_base:start().
% <0.84.0>
% 3> count_nitrogenous_base:rpc({count_atcg, 0,0,0,0, "atcg"}).
% [1,1,1,1]
% 4> count_nitrogenous_base:rpc({"atcg"}).
% {error,{"atcg"}}
% 5> exit(whereis(serverproc), kill).
% true
% 6> count_nitrogenous_base:rpc({count_atcg, 0,0,0,0, "atcga"}).
% [2,1,1,1]