Created
January 30, 2012 18:00
-
-
Save pfiled/1705685 to your computer and use it in GitHub Desktop.
week 3 erlang fridge
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
1> fridge2:start_fridge(). | |
<0.37.0> | |
The contents are now: | |
[{bacon,44},{eggs,59}] | |
The contents are now: | |
[{bacon,43},{eggs,58}] | |
The contents are now: | |
[{bacon,43},{eggs,57}] | |
2> fridge2:store(milk, 47). | |
stored | |
The contents are now: | |
[{bacon,43},{milk,46},{eggs,56}] | |
The contents are now: | |
[{bacon,43},{milk,45},{eggs,55}] | |
3> fridge2:list(). | |
[{bacon,43},{milk,45},{eggs,55}] | |
The contents are now: | |
[{bacon,43},{milk,44},{eggs,54}] | |
4> fridge2:unplug(). | |
terminate | |
The compressor has been unplugged! | |
5> % nothing happens | |
5> fridge2:plugin(). | |
<0.45.0> | |
The contents are now: | |
[{bacon,43},{milk,43},{eggs,53}] | |
6> fridge2:take(eggs). | |
eggs | |
The contents are now: | |
[{bacon,43},{milk,43}] | |
The contents are now: | |
[{bacon,43},{milk,43}] | |
The contents are now: | |
[{bacon,43},{milk,43}] | |
The contents are now: | |
[{bacon,43},{milk,43}] | |
7> fridge2:stop_fridge(). | |
terminate | |
The compressor has been unplugged! | |
8> |
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(fridge2). | |
-compile([export_all]). | |
start_fridge() -> | |
spawn(?MODULE, restarter, []). | |
stop_fridge() -> | |
compressor ! terminate, | |
fridge ! terminate. | |
restarter() -> | |
process_flag(trap_exit, true), | |
Pid = spawn_link(?MODULE, fridge, [[{bacon, 45}, {eggs, 60}]]), | |
register(fridge, Pid), | |
plugin(), | |
receive | |
{'EXIT', Pid, normal} -> % not a crash | |
ok; | |
{'EXIT', Pid, shutdown} -> % manual termination, not a crash | |
ok; | |
{'EXIT', Pid, _} -> | |
restarter() | |
end. | |
plugin() -> | |
spawn(?MODULE, restart_compressor, []). | |
unplug() -> | |
compressor ! terminate. | |
restart_compressor() -> | |
process_flag(trap_exit, true), | |
Pid = spawn_link(?MODULE, compressor, [5000]), | |
register(compressor, Pid), | |
receive | |
{'EXIT', Pid, normal} -> % not a crash | |
io:format("The compressor has been unplugged!~n"); | |
{'EXIT', Pid, shutdown} -> % manual termination, not a crash | |
ok; | |
{'EXIT', Pid, _} -> | |
restart_compressor() | |
end. | |
compressor(Time) -> | |
Pid = whereis(fridge), | |
receive | |
{Pid, {cooled, FoodList}} -> | |
io:format("The contents are now:~n~p~n", [FoodList]), | |
compressor(Time); | |
terminate -> | |
ok | |
after Time -> | |
fridge ! {self(), {cool, 1}}, | |
compressor(Time) | |
end. | |
take(Food) -> | |
fridge ! {self(), {take, Food}}, | |
Pid = whereis(fridge), | |
receive | |
{Pid, {ok, Food}} -> Food | |
after 2000 -> | |
timeout | |
end. | |
list() -> | |
fridge ! {self(), list}, | |
Pid = whereis(fridge), | |
receive | |
{Pid, Food} -> Food | |
after 2000 -> | |
timeout | |
end. | |
store(Food, Temp) -> | |
fridge ! {self(), {store, {Food, Temp}}}, | |
Pid = whereis(fridge), | |
receive | |
{Pid, {ok}} -> stored | |
after 2000 -> | |
timeout | |
end. | |
fridge(FoodList) -> | |
receive | |
{From, {store, {Food, Temp}}} -> | |
From ! {self(), {ok}}, | |
fridge([{Food, Temp}|FoodList]); | |
{From, {take, Food}} -> | |
case lists:keyfind(Food, 1, FoodList) of | |
{Food,Temp} -> | |
From ! {self(), {ok, Food}}, | |
fridge(lists:delete({Food,Temp}, FoodList)); | |
false -> | |
From ! {self(), not_found}, | |
fridge(FoodList) | |
end; | |
{From, {cool, Degrees}} -> | |
Cooled = [{Item, Temp - Degrees} || {Item, Temp} <- FoodList, Temp > 43], | |
Cold = [{Item, Temp} || {Item, Temp} <- FoodList, Temp =< 43], | |
NewContents = lists:flatten([Cold|Cooled]), | |
From ! {self(), {cooled, NewContents}}, | |
fridge(NewContents); | |
{From, list} -> | |
From ! {self(), FoodList}, | |
fridge(FoodList); | |
terminate -> | |
ok | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment