[erlang-questions] strange behaviour of gb_trees

Ulf Wiger <>
Sun May 29 14:00:50 CEST 2011


On 29 May 2011, at 10:02, Nico Meyer wrote:

> This is why weakly typed languages require stronlgy disciplined minds :-). Not sure if the reverse is also true.

Heh, in this case, I think the discipline required isn't so daunting. The rule of thumb is that you use a module to represent an abstract datatype (in this case, orddict), and then use the API provided rather than trying to glean details about the (usually opaque) data and then assuming that other functions will work just as well.  :)

I could have added that lists sort will happily sort lists containing objects of any type, whereas ordsets:from_list/1 requires the input to consist of tuples of arity 2, and nothing else.

BR,
Ulf W 

> 
> On 28.05.2011 20:17, Ulf Wiger wrote:
>> 
>> On 28 May 2011, at 00:39, Nico Meyer wrote:
>> 
>>> Try instead
>>> 1>  T = gb_trees:from_orddict( lists:sort(P) ).
>> 
>> To maintain the abstraction, it should be
>> 
>> 1>  T = gb_trees:from_orddict( orddict:from_list(P) ).
>> 
>> Note that orddicts are not just sorted lists - they are sorted {Key, Value} lists where each key occurs at most once.
>> 
>> Note also:
>> 
>> 2>  orddict:from_list([{a,1},{a,2}]).
>> [{a,2}]
>> 3>  lists:ukeysort(1,[{a,1},{a,2}]).
>> [{a,1}]
>> 
>> Again, stick to the abstraction wherever possible, in order to avoid surprises.
>> 
>> BR,
>> Ulf W
>> 
>> Ulf Wiger, CTO, Erlang Solutions, Ltd.
>> http://erlang-solutions.com
>> 
>> 
>> 

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com






More information about the erlang-questions mailing list