Last active
December 22, 2015 17:29
-
-
Save practice/6506533 to your computer and use it in GitHub Desktop.
erlang 프로그래밍 템플릿
This file contains hidden or 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
-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. |
This file contains hidden or 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
-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