pyrsistent icon indicating copy to clipboard operation
pyrsistent copied to clipboard

PMap values, keys, and items should be iterables in Python 3

Open mitar opened this issue 6 years ago • 3 comments
trafficstars

Currently they wrap them into pvector which is not as efficient as it could be in Python 3.

mitar avatar Mar 29 '19 17:03 mitar

Thanks for reporting this! I'm happy to take a PR fixing this. Preferably including some benchmarks of before and after to justify any increase in code complexity (that may arise from supporting both Python 2 and 3).

tobgu avatar Apr 01 '19 20:04 tobgu

This is not just a matter of efficiency, but of semantics. In Python 3, dict keys are considered set-like. In particular, their order isn't considered when comparing for equality.

Currently, PMap's keys are PVectors, which means the keys of two mappings compare not equal whereas the keys for equivalent dicts compare equal. For example, for a Python 3 user, this behavior is unexpected:

In [7]: a = {'x': 1, 'y': 2}                                                                                                                                                                                                                                                                           

In [8]: b = {'y': 2, 'x': 1}                                                                                                                                                                                                                                                                           

In [9]: a.keys() == b.keys()                                                                                                                                                                                                                                                                           
Out[9]: True

In [10]: pyrsistent.pmap(a).keys() == pyrsistent.pmap(b).keys()                                                                                                                                                                                                                                        
Out[10]: False

jtrakk avatar Sep 05 '19 19:09 jtrakk

Interesting. I would suggest you open another issue for this (keys not being equal because of the order) though. Or we should expand this issue that it is not just about a question of making keys iterable, but also a special object which has its own equality).

mitar avatar Sep 05 '19 19:09 mitar