Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang Dictionaries

I think I know the answer already, but wanted to make sure. I'm constructing a dictionary to serve as a static lookup table (i.e., the dictionary will be immutable once it's created), and have found that this serves the purpose:

L = [{keyA, "A"}, {keyB, "B"}, {keyC, "C"}].
D = dict:from_list(L).
V = dict:fetch(keyA, D).

Is this an acceptable method or is there some other magic I'm not yet familiar with?

As a follow-up, if I were creating a mutable dictionary, does one really have to entertain machinations such as

D  = dict:from_list(L).
D1 = dict:append(keyD, "D", D).

And finally, rather than passing the dictionary around from function to function, is there a persistent store I can stash and retrieve it from, along the lines of the register/2-whereis/1 routine?

like image 846
Joe Avatar asked Jul 12 '12 21:07

Joe


1 Answers

Yes, that is an acceptable method.

If you want to modify your dictionary, you always need to get the return values from all operations which modify your dictionary, as Erlang only has immutable data. This is the normal way to handle all Erlang data, so you will very quickly get used to it.

An alternative is to use ets which is another way of storing data. Depending on how you create an ets table you may or may not need to carry around a reference to it. The data in ets tables is not stored in the process heap so accessing it entails copying between the process and the ets data, however ets tables are generally better at storing large amounts of data. Whether it is better to use dict/orddict/gb_trees or ets depends very much on the data you intend to store and what operations you mean to do on it.

like image 148
rvirding Avatar answered Sep 23 '22 02:09

rvirding