Created
July 10, 2011 20:22
-
-
Save bluegraybox/1074932 to your computer and use it in GitHub Desktop.
Erlang countdown timer which demonstrates code reloading
This file contains 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(countdown). | |
-export([init/0, reload/0]). | |
-export([tick/1]). % so we can spawn this properly. | |
%% spawn a countdown process with a default start time of 10 seconds. | |
init() -> init(10). | |
init(Time) -> | |
register(ticker, spawn(?MODULE, tick, [Time])). | |
tick(Time) when Time >= 0 -> | |
io:format("Tick ~p~n", [Time]), | |
timer:sleep(999), | |
receive reload -> | |
?MODULE:tick(Time - 1) | |
after 1 -> tick(Time - 1) | |
end; | |
tick(_StartTime) -> | |
io:format("Boom.~n", []), | |
done. | |
reload() -> | |
ticker ! reload. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, if you want to see the "no brakes" version, it's:
In this case, the output looks like:
Whoa! The code updated as soon as it was loaded. Slick, but maybe a little too slick...