[erlang-questions] Comparing arbitrary-sized tuples for equality ?

ludovic <>
Tue Oct 8 12:27:22 CEST 2013


Hi,

In short, i would have some advice about lists vs tuples vs ? for
storing hierachical-path data.

I'm working on an application where i have to handle items and item
groups. In some cases, i must check the group of an item.

If at some point i need an item of group
group1>subgroup1>subsubgroup1, an item from the group
group1>subgroup1>subsubgroup1>deepgroup1 is accepted but an item from
group1>subgroup1 isn't because it is not specialized enough.

So, at the moment i use lists of atoms to implement groups :

     Requirements = [ [group1,subgroup1,subsubgroup1]
                    , [group1,subgroup2,othersubsub90]
                    ],
     MyItem = [ {group,[group1,subgroup1,subsubgroup1,deepgroup1]},
              , {id, 123}
              , {name, <<"myitem">>}
              ],

This is easy because if MyItem's group contains (or equals) any of the
Requirements the item is accepted. If no requirement is contained by
the item group, the item is rejected : (i'm not writing the base cases
there)

     check(ItemGroup,[R|Requirements]) ->
         case lists:sublist(ItemGroup,length(R))
             of R -> ok
              ; _ -> not_so_ok
         end

I would like to know if it's a good solution. I have to store items
and requirements in mnesia, and groups will be most of the time
between 2 and 4 elements. I believe that tuples are more efficients
for storage and retrieval, but i'll have to convert them to lists in
order to compare requirements and groups of any size.

Is it just a matter of what is computed the most : (mnesia work or 
checking) ?

What do you think ?

Thanks for reading,

Ludovic

(this is just an example, the wording "requirement" is not really 
suited here)



More information about the erlang-questions mailing list