You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportIterableimportimmutables# Design choices:# - Using a class to allow for typing# - The class has no methods to ensure all logic is in the functions below.# - The wrapped map is kept private.# - To prevent the user from making subtle mistake, we override `__eq__` to raise an error.# Corollaries:# - Will not work with operators ootb, e.g. `in`, `==` or `len`.classImmutableSet:
def__init__(self, inner):
self._inner=innerdef__eq__(self, _):
raiseNotImplementedError(
"Use the functions in this module instead of operators.",
)
defcreate(iterable: Iterable) ->ImmutableSet:
returnImmutableSet(immutables.Map(map(lambdax: (x, None), iterable)))
EMPTY: ImmutableSet=create([])
defequals(s1: ImmutableSet, s2: ImmutableSet) ->bool:
returns1._inner==s2._inner# noqa: SF01deflength(set: ImmutableSet) ->int:
returnlen(set._inner) # noqa: SF01defadd(set: ImmutableSet, element) ->ImmutableSet:
returnImmutableSet(set._inner.set(element, None)) # noqa: SF01defremove(set: ImmutableSet, element) ->ImmutableSet:
returnImmutableSet(set._inner.delete(element)) # noqa: SF01defcontains(set: ImmutableSet, element) ->bool:
returnelementinset._inner# noqa: SF01defunion(set1: ImmutableSet, set2: ImmutableSet) ->ImmutableSet:
smaller, larger=sorted([set1, set2], key=length)
returnImmutableSet(larger._inner.update(smaller._inner)) # noqa: SF01defintersection(set1: ImmutableSet, set2: ImmutableSet) ->ImmutableSet:
smaller, larger=sorted([set1, set2], key=length)
forelementinsmaller._inner: # noqa: SF01ifnotcontains(larger, element):
smaller=remove(smaller, element)
returnsmaller
if you do here it is:
and tests:
The text was updated successfully, but these errors were encountered: