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

Jonathan Leivent jleivent@REDACTED
Fri May 31 00:17:37 CEST 2013


I noticed that a lot of the opaque data structures don't have an 
is_empty predicate.  What is the best way to test if one of these is 
empty?  Preferably an O(1) test, not size(X)==0 (unless that is O(1)), 
because the structure might be quite large.

One way I thought of is to use fold on a function that just throws:

   is_empty(Dict) ->
     catch dict:fold(fun (_,_) -> throw(false) end, true, Dict).

But that is disturbingly ugly.  Is there a preferred way to do this?

Thanks.
Jonathan Leivent



More information about the erlang-questions mailing list