diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
commit | 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch) | |
tree | 982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/kitchen.erl | |
parent | 05ae56e5e89abf2993f84e6d52b250131f247c35 (diff) | |
download | erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2 erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip |
wip
Diffstat (limited to 'learn-you-some-erlang/kitchen.erl')
-rw-r--r-- | learn-you-some-erlang/kitchen.erl | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/learn-you-some-erlang/kitchen.erl b/learn-you-some-erlang/kitchen.erl new file mode 100644 index 0000000..bbe0271 --- /dev/null +++ b/learn-you-some-erlang/kitchen.erl @@ -0,0 +1,64 @@ +-module(kitchen). +-compile(export_all). + +start(FoodList) -> + spawn(?MODULE, fridge2, [FoodList]). + +store(Pid, Food) -> + Pid ! {self(), {store, Food}}, + receive + {Pid, Msg} -> Msg + end. + +take(Pid, Food) -> + Pid ! {self(), {take, Food}}, + receive + {Pid, Msg} -> Msg + end. + +store2(Pid, Food) -> + Pid ! {self(), {store, Food}}, + receive + {Pid, Msg} -> Msg + after 3000 -> + timeout + end. + +take2(Pid, Food) -> + Pid ! {self(), {take, Food}}, + receive + {Pid, Msg} -> Msg + after 3000 -> + timeout + end. + +fridge1() -> + receive + {From, {store, _Food}} -> + From ! {self(), ok}, + fridge1(); + {From, {take, _Food}} -> + %% uh.... + From ! {self(), not_found}, + fridge1(); + terminate -> + ok + end. + +fridge2(FoodList) -> + receive + {From, {store, Food}} -> + From ! {self(), ok}, + fridge2([Food|FoodList]); + {From, {take, Food}} -> + case lists:member(Food, FoodList) of + true -> + From ! {self(), {ok, Food}}, + fridge2(lists:delete(Food, FoodList)); + false -> + From ! {self(), not_found}, + fridge2(FoodList) + end; + terminate -> + ok + end. |