def apply(din: Maybe, *, f, args=None): maybe_args = [] maybe_kwds = {} if isinstance(args, (list, tuple)): for a in args: maybe_args.append(sync_with(din, a)) elif isinstance(args, dict): for n, v in args.items(): maybe_kwds[n] = sync_with(din, v) if maybe_args or maybe_kwds: return din | unionmap(f=(None, f(*maybe_args, **maybe_kwds))) | Maybe else: return din | unionmap(f=(None, f)) | Maybe
def ucase(din: Union, *, f, fcat=ccat, tout=None, fmux=mux, mapping=None): dout = din \ | unionmap(f=f, fmux=fmux, mapping=mapping, use_dflt=False) if dout.dtype.types.count(dout.dtype.types[0]) != len(dout.dtype.types): raise TypeMatchError( f'output types of all input types need be the same, but got "{dout.dtype.types}' ) return dout | union_collapse(t=tout)
def case(cond, din, *, f, fcat=ccat, tout=None, **kwds): try: len(f) except TypeError: f = (None, f) return fcat(din, cond) \ | Union \ | unionmap(f=f, **kwds) \ | union_collapse(t=tout)
def case(cond, din, *, f, fcat=ccat, tout=None, mapping=None, **kwds): try: len(f) except TypeError: f = (None, f) if mapping is None: in_num_types = len(f) else: in_num_types = max(mapping.keys()) + 1 return fcat(din, cond) \ | Union[(din.dtype, ) * in_num_types] \ | unionmap(f=f, mapping=mapping, **kwds) \ | union_collapse(t=tout)
def maybe_when(cond, din, *, f, fcat=ccat): return fcat(din, cond) \ | Union \ | unionmap(f=(fix(val=Unit()), f)) \ | Maybe