Skip to content

Instantly share code, notes, and snippets.

@practice
Last active December 22, 2015 17:29
Show Gist options
  • Save practice/6506533 to your computer and use it in GitHub Desktop.
Save practice/6506533 to your computer and use it in GitHub Desktop.
erlang 프로그래밍 템플릿
-module(ctemplate).
-compile(export_all).
start() ->
spawn(fun() -> loop([]) end).
loop(X) ->
receive
Any ->
io:format("Received: ~p~n", [Any]),
loop(X)
end.
rpc(Pid, Request) ->
Pid ! {self(), Request},
receive
{Pid, Response} ->
Response
end.
-module(kvs).
-export([start/0, store/2, lookup/1]).
start() -> register(kvs, spawn(fun() -> loop() end)).
store(Key, Value) -> rpc({store, Key, Value}).
lookup(Key) -> rpc({lookup, Key}).
rpc(Q) ->
kvs ! {self(), Q},
receive
{kvs, Reply} ->
Reply
end.
loop() ->
receive
{From, {store, Key, Value}} ->
put(Key, {ok, Value}),
From ! {kvs, true},
loop();
{From, {lookup, Key}} ->
From ! {kvs, get(Key)},
loop()
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment