Added FSet and fset

This commit is contained in:
Emi Simpson 2023-03-06 11:31:39 -05:00
parent f0d7ee2d5e
commit 65a8c0042f
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -1,7 +1,7 @@
from dataclasses import dataclass
from functools import partial, wraps
from operator import not_
from typing import Any, Callable, Concatenate, Generic, Iterable, Iterator, List, ParamSpec, Sequence, Tuple, Type, TypeGuard, TypeVar
from typing import Any, Callable, Concatenate, Generic, FrozenSet, Iterable, Iterator, List, ParamSpec, Sequence, Tuple, Type, TypeGuard, TypeVar
A = TypeVar('A')
B = TypeVar('B')
@ -168,6 +168,22 @@ def p_instance(c: Type[A]) -> Callable[[Any], TypeGuard[A]]:
"A curried version of the built in `is_instance` function"
return flip(cur2(isinstance))(c) #type: ignore
class FSet(FrozenSet[A]):
"A subclass of FrozenSet with a more succinct and deterministic __repr__"
def __repr__(self):
"""
>>> repr(FSet([1, 2, 3]))
'{ 1, 2, 3 }'
"""
if len(self) == 0:
return '{ }'
else:
return '{ ' + ', '.join(sorted([repr(e) for e in self])) + ' }'
def fset(*args: A) -> FSet[A]:
"Alias for `frozenset()` which uses varargs and returns `FSet`"
return FSet(args)
# Normal Accessors
@cur2
def indx(i: int, s: Sequence[A]) -> A: