Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 5-Erlang/infinirun.erl @ 90:c27c87cd0f08
Add most of the Day 2 exercises for Haskell
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 16 Jun 2019 21:09:33 +0100 |
parents | 7bab4843aec6 |
children |
rev | line source |
---|---|
77
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 -module(infinirun). |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 -export([loop/0]). |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 % To run, call: Restarter = spawn(fun infinirun:loop/0). |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 % Then call Restarter ! new to initiate |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 % This does black magic and registers a public atom called "revolver", which is a process running roulette:loop/0 that can receive messages |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 loop() -> |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 % Indicate that we want exist signals, which are a 3-tuple of { 'EXIT', From, Reason } |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 process_flag(trap_exit, true), |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 receive |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 new -> |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 % Start a new process |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 io:format("Creating and monitoring process.~n"), |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 register(revolver, spawn_link(fun roulette:loop/0)), |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 loop(); |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 {'EXIT', From, Reason} -> |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 io:format("The shooter ~p died with reason ~p. Restarting…~n", [From, Reason]), |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 % Send ourselves a message to register a new process |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 self() ! new, |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 loop() |
7bab4843aec6
Add roulette with auto-restarting process
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 end. |