I am writing a cache gen-server for the company Use. I am wondering how to search an item from the list as I want the cost of the search for comparing various data structures in erlang like dict, orddict, List, tuples, tree, queue etc to use for cache program.
Example:
List = [{"A1",["ankit","sush", "Hover", "x4", "a3","nilesh","mike","erlang" | ...]}|...].
Now, I want to search for the Key A1 and search for 'mike' in the list. What is the best way to search the above List.
Please provide some examples. Atleast Pseudo for it.
Thanks for the Answers. I have written a Erlang code to find the timings for Inserting & Fetching Date into various data Structures like LIST, DICT, QUEUE, SET and PARALLEL MAP of the data structures. Only list's insert & fetch is completed but rest has parallel map insert. I would like to share the code & results of the code. Following is the module test.erl
Following are the results: For parallel Map DS
For All DS
Hope this information helps you to determine which is the best DS for using for different purpose. I'll update it when I am finished with whole code.
If your list is a simple one Term based item list, then the simplest solution is:
The above will work assuming that how you check the equality of your items in the list is with the == operator. You can modify that operator for accomodating other types of comparison/equality as you need.
If you want to search a list use the functions in the list module which is part of the extensive documentation that comes with Erlang.
If you want to know the best data structures to use - that's a slightly different question which will require a bit more information.
Just to simplify on the example at https://stackoverflow.com/a/15587565/56250:
lists:member
works with empty lists. Quick look at the source (https://github.com/erlang/otp/blob/07b8f441ca711f9812fad9e9115bab3c3aa92f79/erts/emulator/beam/erl_bif_lists.c#L184) suggests it executes lazily.You can also use
any
from http://erlang.org/doc/man/lists.html#any-2will return
true
if found orfalse
otherwise.Use
keyfind
function fromlists
module. For example: