Last active
August 29, 2015 14:23
-
-
Save dekokun/fbfbdca8f7ac2ff1778b to your computer and use it in GitHub Desktop.
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
http://www.slideshare.net/sleepy_yoshi/erlang-1 を見る | |
c(normal). | |
Pid = normal:start_stack(). | |
normal:get_stack(Pid). | |
normal:push(Pid, hoge). | |
normal:get_stack(Pid). | |
normal:push(Pid, hoge2). | |
normal:get_stack(Pid). | |
normal:pop(Pid). | |
normal:get_stack(Pid). | |
get_stackあたりに | |
io:format("new logging !~n"), | |
とでも追加して、 | |
c(normal). | |
その後、 | |
normal:get_stack(Pid). | |
プログラムは変更したが状態が失われていない!! | |
結構面倒くさそうなところが多いとは思いますが、OTPフレームワークというやつを使うとこのあたりが非常に分かりやすく書き換えられる | |
c(otp_test). | |
{ok, Pid2} = otp_test:start_stack(). | |
otp_test:get_stack(Pid2). | |
otp_test:push(Pid2, hoge). | |
otp_test:get_stack(Pid2). | |
otp_test:pop(Pid2). | |
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(normal). | |
-export([start_stack/0, stack/1, get_stack/1, push/2, pop/1]). | |
start_stack() -> | |
spawn(?MODULE, stack, [[]]). | |
get_stack(Pid) -> | |
io:format("new logging !~n"), | |
Pid ! {all, self()}, | |
receive | |
Item -> Item | |
end. | |
push(Pid, Item) -> | |
Pid ! {push, Item}. | |
pop(Pid) -> | |
Pid ! {pop, self()}, | |
receive | |
Item -> Item | |
end. | |
stack(Stack) -> | |
receive | |
{push, Item} -> | |
stack([Item|Stack]); | |
{pop, From} -> | |
[Item|After] = Stack, | |
From ! Item, | |
stack(After); | |
{all, From} -> | |
From ! Stack, | |
stack(Stack) | |
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(otp_test). | |
-behaviour(gen_server). | |
-export([start_stack/0, get_stack/1, push/2, pop/1]). | |
-export([init/1, handle_call/3, handle_cast/2, code_change/3, terminate/2, handle_info/2]). | |
start_stack() -> | |
gen_server:start_link(?MODULE, [], []). | |
get_stack(Pid) -> | |
gen_server:call(Pid, all). | |
push(Pid, Item) -> | |
gen_server:cast(Pid, {push, Item}). | |
pop(Pid) -> | |
gen_server:call(Pid, pop). | |
init(_Args) -> | |
{ok, []}. | |
handle_cast({push, Item}, Stack) -> | |
{noreply, [Item|Stack]}. | |
handle_call(pop, _From, Stack) -> | |
[Item|After] = Stack, | |
{reply, Item, After}; | |
handle_call(all, _From, Stack) -> | |
{reply, Stack, Stack}. | |
code_change(_, _, _) -> | |
ok. | |
handle_info(Msg, State) -> | |
io:format("Unexpected message: ~p~n",[Msg]), | |
{noreply, State}. | |
terminate(_, _) -> | |
ok. |
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
defmodule OtpTest do | |
@behaviour :gen_server | |
def start_stack() do | |
:gen_server.start_link(__MODULE__, [], []) | |
end | |
def get_stack(pid) do | |
:gen_server.call(pid, :all) | |
end | |
def push(pid, item) do | |
:gen_server.cast(pid, {:push, item}) | |
end | |
def pop(pid) -> | |
:gen_server.call(pid, :pop) | |
end | |
def init(_Args) do | |
{:ok, []} | |
end | |
def handle_cast({:push, item}, stack) do | |
{:noreply, [item|stack]} | |
end | |
def handle_call(:pop, _from, stack) do | |
[ item | after ] = stack | |
{:reply, item, after} | |
end | |
def handle_call(all, _from, stack) do | |
{:reply, stack, stack} | |
end | |
def code_change(_, _, _) do | |
:ok | |
end | |
def handle_info(msg, state) do | |
:io.format("Unexpected message: ~p~n",[msg]) | |
{:noreply, state}. | |
end | |
def terminate(_, _) do | |
ok | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment