75 lines
No EOL
2.5 KiB
Python
75 lines
No EOL
2.5 KiB
Python
# ITHON START
|
|
import typing as _INTERNAL_typing
|
|
import types as _INTERNAL_types
|
|
import inspect as _INTERNAL_inspect
|
|
import sys as _INTERNAL_sys
|
|
class _INTERNAL_Token:
|
|
__slots__ = ('action',)
|
|
|
|
def __init__(self, action):
|
|
self.action = action
|
|
|
|
def __rrshift__(self, lhs):
|
|
return _INTERNAL_Operation(self.action, lhs)
|
|
|
|
class _INTERNAL_Operation:
|
|
__slots__ = ('action', 'lhs')
|
|
|
|
def __init__(self, action, lhs):
|
|
self.action = action
|
|
self.lhs = lhs
|
|
|
|
def __lshift__(self, rhs):
|
|
return self.action(self.lhs, rhs)
|
|
|
|
class _INTERNAL_LazyIterable:
|
|
__slots__ = ('x','y')
|
|
def __init__(self, x, y) -> None:
|
|
self.x = iter(x)
|
|
self.y = iter(y)
|
|
def __iter__(self):
|
|
yield from self.x
|
|
yield from self.y
|
|
|
|
def curry(f):
|
|
def wrapper(*args, **kwds) -> _INTERNAL_typing.Any | _INTERNAL_typing.Callable:
|
|
signature = _INTERNAL_inspect.signature(f)
|
|
ba = signature.bind_partial(*args, **kwds)
|
|
if len(ba.arguments) == len(signature.parameters):
|
|
return f(*args, **kwds)
|
|
else:
|
|
def _partial(*other_args, **other_kwds):
|
|
combined_args = args + other_args
|
|
combined_kwargs = dict(**kwds, **other_kwds)
|
|
return curry(f)(*combined_args, **combined_kwargs)
|
|
return _partial
|
|
return wrapper
|
|
|
|
def _INTERNAL_add_fakeimport(name: str, code: str): # TODO: make this use sys.meta_path
|
|
module = _INTERNAL_types.ModuleType(name)
|
|
parent = '.'.join(name.split('.')[:-1]) if '.'.join(name.split('.')[:-1]) else name
|
|
if not parent == name and parent not in _INTERNAL_sys.modules:
|
|
_INTERNAL_sys.modules[parent] = _INTERNAL_types.ModuleType(parent)
|
|
globals = {'__package__': parent}
|
|
module.__dict__.update(globals)
|
|
exec(code, module.__dict__)
|
|
_INTERNAL_sys.modules[name] = module
|
|
|
|
_INTERNAL_add_fakeimport('sentinels', std'sentinels.py')
|
|
_INTERNAL_lazymerge = _INTERNAL_Token(lambda lhs, rhs: _INTERNAL_LazyIterable(lhs, rhs))
|
|
|
|
_INTERNAL_lpipe = _INTERNAL_Token(lambda lhs, rhs: rhs(lhs))
|
|
_INTERNAL_rpipe = _INTERNAL_Token(lambda lhs, rhs: lhs(rhs))
|
|
_INTERNAL_lspipe = _INTERNAL_Token(lambda lhs, rhs: rhs(*lhs))
|
|
_INTERNAL_rspipe = _INTERNAL_Token(lambda lhs, rhs: lhs(*rhs))
|
|
|
|
_INTERNAL_nonereplace = _INTERNAL_Token(lambda lhs, rhs: lhs if lhs != None else rhs)
|
|
|
|
_INTERNAL_lto = _INTERNAL_Token(lambda lhs, rhs: lhs(*rhs))
|
|
|
|
# If you write in other programming languages, this is very, very useful.
|
|
null = None
|
|
nil = None
|
|
void = None
|
|
|
|
# ITHON END |