Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upThere is no O(1) way of checking if a dict is nonempty #251
Comments
imeckler
changed the title from
There is no O(1) way of checking if a dict is empty
to
There is no O(1) way of checking if a dict is nonempty
May 23, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lachenmayer
May 24, 2015
Looking at the source, it seems like isEmpty x = x == Dict.empty should always work.
The reason why the documentation states that "Dictionary equality with (==) is unreliable and should not be used" is that a Dict is a self-balancing red-black tree, and the distribution of red and black nodes depends on the insertion order.
That being said:
Dict.empty is defined as RBEmpty LBlack.
From a non-empty tree, the only way to arrive at an empty Dict is to remove an element.
Every remove operation performs blacken (here).
blacken will result in RBEmpty LBlack if the tree is empty (here).
I've only glanced at the code briefly, do let me know if you find a counter-example.
You're right though, Dict should have an isEmpty function, since List and String both contain one.
lachenmayer
commented
May 24, 2015
|
Looking at the source, it seems like The reason why the documentation states that "Dictionary equality with (==) is unreliable and should not be used" is that a Dict is a self-balancing red-black tree, and the distribution of red and black nodes depends on the insertion order.
I've only glanced at the code briefly, do let me know if you find a counter-example. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
May 25, 2015
Member
I think the reason this function does not exist is because I wanted a uniform API across all the containers, but wasn't sure what that name should be / what all these libraries would look like. I think isEmpty is a nice way to go, so I think it's cool to add that as long as we do it in List, Dict, Set, and Array.
Can someone do a PR for this?
|
I think the reason this function does not exist is because I wanted a uniform API across all the containers, but wasn't sure what that name should be / what all these libraries would look like. I think Can someone do a PR for this? |
AlexNisnevich
referenced this issue
May 26, 2015
Merged
O(1) methods for Array.isEmpty, Dict.isEmpty, and Set.isEmpty #253
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
I think this issue can be closed. |
imeckler commentedMay 23, 2015
You could do
Dict.foldl (\_ _ _ -> True) False, but this is O(n).