Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bags to accept non-hashable elements #4

Open
mlenzen opened this issue Aug 17, 2013 · 3 comments
Open

Bags to accept non-hashable elements #4

mlenzen opened this issue Aug 17, 2013 · 3 comments

Comments

@mlenzen
Copy link
Owner

mlenzen commented Aug 17, 2013

Bags should accept non-hashable elements. Currently a bag is backed by a
dict, so you can't add a bag to another bag.

@ghost ghost assigned mlenzen Aug 17, 2013
@mlenzen mlenzen added enhancement and removed bug labels Jan 20, 2015
@mlenzen mlenzen changed the title Bags don't accept non-hashable elements Bags to accept non-hashable elements Jan 20, 2015
@mlenzen
Copy link
Owner Author

mlenzen commented Jan 20, 2015

Maybe, add a list to the backing. Whenever a mutable elem is added/accessed, it would fall back to the list.

@mlenzen mlenzen added the api label Jan 21, 2015
@mlenzen mlenzen modified the milestone: 1.0 Jan 21, 2015
@mlenzen mlenzen removed this from the 1.0 milestone Mar 31, 2016
@mlenzen
Copy link
Owner Author

mlenzen commented Nov 5, 2018

I think this does make sense, it won't reduce performance, it will just make bag more flexible.

@mlenzen mlenzen added this to the 2.0 milestone Aug 17, 2019
@mlenzen mlenzen removed this from the 1.1 milestone Aug 20, 2021
@mlenzen mlenzen removed their assignment Sep 14, 2021
@joeystevens00
Copy link

joeystevens00 commented Sep 21, 2021

I think if bag isn't hashable then it can't support checking equality of nested objects when they're not ordered the same way. The underlying data type as a list or a tuple means insertion ordering will be preserved and considered when checking equality. I can't seem to find a way to validate that two unordered nested lists are equal but the bags they represent very well might be.

This means this will be valid:

b = frozenbag([1, 1, 2, 3])	 # prototypical frozen multiset
c = frozenbag([4, 4, 5, 5, b, b])  # make sure we can nest them
d = frozenbag([4, frozenbag([1, 1, 2, 3]), 4, 5, b, 5])
assert bag([4, 4, 5, 5, c]) == bag([4, 5, d, 4, 5])

But this won't because the different ordering of b([1,3,2,1]) causes the data structure to be different than in the child object in d([1,1,2,3]). Both bags contain the same number of elements and are equal when normally checked together but not here:

b = frozenbag([1, 3, 2, 1])	 # prototypical frozen multiset
c = frozenbag([4, 4, 5, 5, b, b])  # make sure we can nest them
d = frozenbag([4, frozenbag([1, 1, 2, 3]), 4, 5, b, 5])
assert bag([4, 4, 5, 5, c]) == bag([4, 5, d, 4, 5])

https://github.com/mlenzen/collections-extended/compare/master...joeystevens00:bag_support_non_hashable_elements?expand=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants