[erlang-questions] Best way to test if empty dict/set/etc.?

Jonathan Leivent jleivent@REDACTED
Fri May 31 16:44:26 CEST 2013


On 05/31/2013 03:25 AM, Kostis Sagonas wrote:
> ...
> Since I've also stumbled across something analogous in the past, IMO the
> proper solution is to extend the API of these opaque data structures
> (these modules) by providing is_empty/1 functions for them.

I feel better knowing that I wasn't just missing some obvious is_empty 
substitute.  I also agree with your sentiment about keeping opaque 
structures "opaque" - and the documentation implies a similar sentiment 
("The representation of a dictionary|set is not defined.").

In an off-list post, it was pointed out that the structures that are 
missing is_empty predicates (dict, orddict, sets, ordsets) are also 
missing any API function to peek at or take an arbitrary element 
(similar to queue:peek or queue:out) - which could be used as an 
is_empty substitute (a more useful one at that).

Another problem with relying on either size(X) or the representation 
itself is that dict and orddict (likewise set and ordset) have identical 
APIs apparently to allow for substitution of one for the other with 
minimal code churn (other than the == vs. =:= issue).  But their 
representations (and the performance of size) differ.

> I am sure the OTP folks would welcome a patch.

Maybe that's the answer.  But, the lack of an is_empty predicate for 
dict/sets has existed in Erlang for so long!?

-- Jonathan




More information about the erlang-questions mailing list