1> % first compile frequency using contents of frequency2.erl
1> c(frequency).
{ok,frequency}
2> frequency:start().
true
3> frequency:allocate().
{ok,10}
4> frequency:allocate().
{ok,11}
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
(* a stream is a thunk that when called produces a pair | |
#1 of the pair will be of type 'a and #2 of the pair will be another stream *) | |
(* can only have recursive type using datatype binding *) | |
datatype 'a StreamPair = Stream of 'a * (unit -> 'a StreamPair) | |
(* a ones stream *) | |
fun ones () = Stream (1, ones) | |
(* a fibonacci number stream *) | |
fun fibs () = |
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
%% Based on code from | |
%% Erlang Programming | |
%% Francecso Cesarini and Simon Thompson | |
%% O'Reilly, 2008 | |
%% http://oreilly.com/catalog/9780596518189/ | |
%% http://www.erlangprogramming.org/ | |
%% (c) Francesco Cesarini and Simon Thompson | |
-module(gf). | |
-behaviour(gen_server). |
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(frequency_front). | |
-export([start/0, allocate/0, deallocate/1, stop/0]). | |
-export([init/0]). | |
%% API | |
allocate() -> | |
send_receive(allocate). | |
deallocate(Freq) -> | |
send_receive({deallocate, Freq}). |
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(exceptions). | |
-export([eval/2, wrap/2]). | |
% simple model of arithmetical expressions, like | |
% {add, {num, 0}, {mul, {num,1}, {var,a2}}} | |
% environment gives value of a variable: | |
% [ {a,2}, {b,1} ] | |
eval(_Env, {num, N}) -> N; |
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(frequency). | |
-export([start/0, allocate/0, deallocate/1, stop/0, init/0]). | |
start() -> | |
Pid = spawn(?MODULE, init, []), | |
register(?MODULE, Pid), | |
Pid. | |
allocate() -> | |
send_receive(allocate). |
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(echo). | |
-export([listener/0]). | |
listener() -> | |
receive | |
{Pid, M} -> | |
io:format("~w echoed.~n", [M]), | |
Pid ! M, | |
listener() | |
end. |
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(frequency_hardened). | |
-export([start/0, allocate/0, deallocate/1, stop/0, init/0]). | |
start() -> | |
Pid = spawn(?MODULE, init, []), | |
register(?MODULE, Pid), | |
Pid. | |
allocate() -> | |
?MODULE ! {request, self(), allocate}, |
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(frequency). | |
-export([start/0, allocate/0, deallocate/1, stop/0, clear/0, clear_with_print/0, init/0]). | |
start() -> | |
Pid = spawn(?MODULE, init, []), | |
register(?MODULE, Pid), | |
Pid. | |
%% Add clear at beginning of each API call to eliminate unexpected messages in mailbox | |
%% better use combination of erlang:make_ref() and "let it fail" |
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(frequency). | |
-export([start/0, init/0]). | |
start() -> | |
Pid = spawn(?MODULE, init, []), | |
register(?MODULE, Pid), | |
Pid. | |
init() -> | |
Frequencies = {get_frequencies(), []}, |
NewerOlder