[erlang-questions] Best way to test if empty dict/set/etc.?
Fri May 31 08:31:38 CEST 2013
Check the implementation of those data structures. There is always a
function that could tell you if it is empty or not. If size would not
be a O(1) call, it would probably say calc_size or something.
dict:size is O(1).
On Fri, May 31, 2013 at 12:17 AM, Jonathan Leivent <jleivent@REDACTED> 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?
> Jonathan Leivent
> erlang-questions mailing list
More information about the erlang-questions