BBVA/mercury-settrie

TreeSet should be iterable, yielding its elements or an empty iterator

Closed this issue · 5 comments

The reason is that calling "treeset.elements" to iterate the elements of a treeset if treeset contains an empty set returns None instead of an empty iterator, thus causing an edge case ugly to handle. It would be better to just iter(treeset) knowing to get an empty iterator if treeset is the empty set.

It would be also fine to return an empty iterator if the treeset contains 0 elements when calling .elements.

(See https://github.com/BBVA/mercury-settrie/blob/features/towards_146/CHANGELOG.md for state updates.)

This also boils down to a pure-Python implementation. I will look at it further. If you can provide some code to trigger the inconsistent behavior and even a fix, please do not hesitate.

from settrie import SetTrie
s1 = SetTrie()
s1.insert({1}, 'a')
list(list(s1)[0].elements) # [1]

s2 = SetTrie()
s2.insert({}, 'b')
list(list(s2)[0].elements) # TypeError: 'NoneType' object is not iterable

But now I understand this comes down to supporting empty sets

When I checked to implement len() I saw that the None logic was forced when I implemented the iterator over the elements. That is artificial and not necessary as you pointed out. The class Result handles the empty return nicely already,

Please, check the branch features/towards_146 and tell me if that already solves the issue.

It fixes the None problem.

If you agree to make TreeSet iterable, you should add __len__ and __iter__ to TreeSet, otherwise simply close this issue as not planned

Fixed in version 1.4.6