Created
March 13, 2009 01:32
-
-
Save devyn/78387 to your computer and use it in GitHub Desktop.
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(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