Skip to content

Instantly share code, notes, and snippets.

@EmmaIreland
EmmaIreland / ngrams.erl
Last active December 15, 2015 21:39
Map-reduce N-grams in Erlang
%Authors: Emma Ireland and John McCall
%Note: For some reason you can't pass a table id to processes, it just doesn't work so we have to create a Table to give it,
%then send it a message to create a new table in its process.
%Note: When calling createProcesses the ListOfHosts needs to have at least 2 arguments (even just [node(), node()]), because
%crypto:rand_uniform(1, length(ListOfHosts)) will return 2 if length of hosts is 1 (really it should throw a badarg exception, but doesn't).
%Note: To see the table at the end, find our aggregator using global:whereis_name and then send it showTable as a message.
@EmmaIreland
EmmaIreland / dist_bakery.erl
Last active December 15, 2015 13:18
The distributed bakery algorithm in Erlang
%Authors: Emma Ireland and John McCall
-module(dist_bakery).
-export([server/0, bake/2, customer/0, manager/2, pair/2, create_people/4, fib/1]).
server() ->
receive
{serveCustomer, CustomerID, ToBake} ->
io:format("Server: ~w is serving customer: ~w~n", [self(), CustomerID]),
bake(CustomerID, ToBake),
server()
@EmmaIreland
EmmaIreland / bakery.erl
Last active June 7, 2021 14:55
The Bakery Algorithm in Erlang
% Authors: Emma Ireland and John McCall
-module(bakery).
-export([server/0, customer/0, manager/2, pair/2, create_people/2, fib/1]).
server() ->
receive
{serveCustomer, CustomerID} ->
io:format("Server: ~w is serving customer: ~w~n", [self(), CustomerID]),
ToBake = crypto:rand_uniform(30, 40),
self() ! {bake, CustomerID, ToBake},