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

Fred Hebert mononcqc@REDACTED
Fri May 31 17:19:57 CEST 2013


Most of these data structures have a 'new' or 'empty'  function call
that creates an empty/blank data structure.

I then just compare things like MyDict =:= dict:new() to know if it's
empty. I'm not aware of a case where this doesn't work, but it makes a
very subtle assumption that *might* break opacity: it assumes that the
data structure doesn't keep internal statistics or traces of content it
has seen before but is no longer there.

Regards,
Fred.

On 05/30, Jonathan Leivent wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list