Skip to content

Instantly share code, notes, and snippets.

@jvantuyl
Created May 10, 2015 05:04
Show Gist options
  • Save jvantuyl/addd231f2395127f6789 to your computer and use it in GitHub Desktop.
Save jvantuyl/addd231f2395127f6789 to your computer and use it in GitHub Desktop.
FizzBuzz
-module(fizzbuzz).
-export([start/0, watcher/0]).
start() ->
Watcher = spawn_link(fizzbuzz, watcher, []),
send_loop(Watcher, 1, 1000000),
receive done -> ok end,
init:stop().
send_loop(Pid, N, Max) when N =< Max ->
Pid ! {fizzbuzz, N},
send_loop(Pid, N+1, Max);
send_loop(Pid, _, _) ->
Pid ! {fizzbuzz, exit_proc, self()},
done.
watcher() ->
receive
{fizzbuzz, I} when I rem 15 == 0 -> ok = io:format("fizzbuzz~n"), watcher();
{fizzbuzz, I} when I rem 3 == 0 -> ok = io:format("fizz~n"), watcher();
{fizzbuzz, I} when I rem 5 == 0 -> ok = io:format("buzz~n"), watcher();
{fizzbuzz, I} when is_number(I) -> ok = io:format("~B~n", [I]), watcher();
{fizzbuzz, exit_proc, Pid} -> Pid ! done, ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment