From 65a8c0042fe81c72077e504552aaf043e90cf8f5 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Mon, 6 Mar 2023 11:31:39 -0500 Subject: [PATCH] Added FSet and fset --- emis_funky_funktions.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/emis_funky_funktions.py b/emis_funky_funktions.py index 929d22a..5227ae2 100644 --- a/emis_funky_funktions.py +++ b/emis_funky_funktions.py @@ -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: