Skip to content

Instantly share code, notes, and snippets.

@devyn
Created March 13, 2009 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devyn/78387 to your computer and use it in GitHub Desktop.
Save devyn/78387 to your computer and use it in GitHub Desktop.
-module(dyndata).
-export([new/0, data_process/1, set_data/2, get_data/1, destroy/1, add/2, at/2]).
%%% Dynamic Data by ~devyn %%%
new() ->
spawn(dyndata, data_process, [[]]).
data_process(DynData) ->
receive
{SendBackTo, set_data, Data} ->
data_process(Data);
{SendBackTo, get_data} ->
SendBackTo ! DynData,
data_process(DynData);
{SendBackTo, destroy} ->
destroyed
end.
set_data(Instance, Data) ->
Instance ! {self(), set_data, Data}, ok.
get_data(Instance) ->
Instance ! {self(), get_data},
receive Data -> Data end.
destroy(Instance) ->
Instance ! {self(), destroy}, ok.
%% OPTIONAL HELPERS %%
add(Instance, Item) ->
set_data(Instance, lists:append(get_data(Instance), [Item])), ok.
at(Instance, Index) ->
lists:nth(Index, get_data(Instance)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment