Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python 3.10+ Two Function Complete Monad Library! :: https://pastebin.com/JP0tdf4s #python #python3 #mod #monad
- # py_struct = NamedTuple
- class CompareResult(py_struct):
- les: bool
- equ: bool
- gtr: bool
- cmp_str_input_t = NewType('cmp_str_input_t', Iterable[SupportsBytes and SupportsIndex and SupportsInt])
- def cmp_iter_str(si1: cmp_str_input_t, si2: cmp_str_input_t) -> CompareResult:
- it1 = iter(si1)
- it2 = iter(si2)
- while True:
- c1 = next(it1, None)
- c2 = next(it2, None)
- n1 = c1 is None
- n2 = c2 is None
- if n1 and n2:
- return CompareResult(False, True, False)
- elif n1 ^ n2:
- return CompareResult(n1, False, n2)
- elif c1 != c2:
- return CompareResult(c1 < c2, False, c1 > c2)
- class monad_attr_err_spec_t(py_struct):
- obj_type: Type[Any]
- attr_name: str
- fxn_triggered: callable
- def monad(call_function: callable, *op_types: monad_attr_err_spec_t, pass_types: bool = False) -> callable:
- def _i(*args, **kwargs):
- try:
- if pass_types:
- return call_function(*args, op_types = op_types, **kwargs)
- return call_function(*args, **kwargs)
- except AttributeError as attr_err: # https://docs.python.org/3.11/library/exceptions.html#AttributeError
- if attr_err.obj is None:
- return
- ae_obj, ae_name = attr_err.obj, attr_err.name # Changed in version 3.10: Added the name and obj attributes.
- x: monad_attr_err_spec_t
- for x in op_types:
- if x.obj_type is type(ae_obj) and ae_name == x.attr_name:
- return x.fxn_triggered(*args, ae_obj = ae_obj, ae_name = ae_name, **kwargs)
- return _i
- py_curry_fxn_arg_t = TypeVar('py_curry_fxn_arg_t')
- py_curry_fxn_closure_t = NewType('py_curry_fxn_closure_t', Callable[[py_curry_fxn_arg_t, ...], py_curry_fxn_arg_t])
- def py_curry(fxn: py_curry_fxn_closure_t, *args: py_curry_fxn_arg_t, **kwargs: py_curry_fxn_arg_t) -> py_curry_fxn_closure_t: # Keep It Simple, Stupid!
- return lambda *args2, **kwargs2: fxn(*args, *args2, **kwargs, **kwargs2) # pass new args2 after old (earlier curried) args and update (earlier) kwargs with kwargs2.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement