def filter(self): l = LazyList(range(30)) l2 = l.filter(_ % 2 == 0) l2.strict.should.have.length_of(0) l3 = LazyList(range(30)) l3[29] l4 = l3.filter(_ % 2 == 0) l4.strict.should.have.length_of(15) l4.drain.should.equal(List.wrap(range(0, 30, 2)))
def _setup_handlers(self): super()._setup_handlers() handlers = inspect.getmembers(self.Transitions, lambda a: hasattr(a, _machine_attr)) handler_map = ( List.wrap(handlers) .smap(lambda n, f: WrappedHandler.create(self, n, self.Transitions, f)) .map(lambda a: (a.message, a)) ) self._message_handlers = self._message_handlers ** handler_map
def effs(self, fa: F, *args): from tryp.eff import Eff types = List.wrap(args) c = L(Eff)(fa, _, depth=_) with_depth = lambda d, t: c(t, depth=d) types_only = lambda: c(types, depth=len(types)) def try_depth(h, t): return with_depth(int(h), t) if isinstance(h, int) else types_only() return types.detach_head.map2(try_depth) | types_only
def __init__( self, fun: Callable[..., Any], name: str=None, nargs=None, min: int=None, max: int=None, **kw ) -> None: self._fun = fun self._argspec = inspect.getfullargspec(fun) # type: ignore self._params = List.wrap(self._argspec.args) self._param_count = self._params.length - ( 1 if self._params.head.contains('self') else 0 ) self._name = Maybe(name) self._nargs = Maybe(try_int(nargs)) self._min = Maybe(min) self._max = Maybe(max) self._kw = kw
def _process_result(self, old_data: Data, result) -> TransitionResult: if isinstance(result, Coroutine): return CoroTransitionResult(data=old_data, coro=result) elif isinstance(result, TransitionResult): return result elif isinstance(result, self._data_type): return TransitionResult.empty(result) elif isinstance(result, Message) or not is_seq(result): result = List(result) datas, rest = List.wrap(result).split_type(self._data_type) strict, rest = rest.split_type(Message) coro, rest = rest.split(iscoroutine) msgs = strict + coro.map(Coroutine).map(_.pub) if rest: tpl = "invalid transition result parts in {}: {}" msg = tpl.format(self.name, rest) if tryp.development: raise MachineError(msg) else: self.log.error(msg) new_data = datas.head | old_data return self._create_result(new_data, msgs)
def _setup_handlers(self): handlers = inspect.getmembers(self, lambda a: hasattr(a, _machine_attr)) handler_map = List.wrap(handlers).smap(Handler.create).map(lambda a: (a.message, a)) self._message_handlers = Map(handler_map)
def decode_list(value): from tryp import List return List.wrap(value).map(decode)
def deep(self): n = int(1e4) l = LazyList(List.wrap(range(n))) l.index_of(n - 1).should.contain(n - 1)
def set(self, value): super().set(value) glob = lambda a: a.parent.glob(a.name) self.value = List.wrap(self.value) / _.value // glob
def _auto_handlers(self, cls): import inspect return List.wrap(inspect.getmembers(cls)).flat_map2(self._auto_handler)
def __init__(self, func, *a, **kw) -> None: assert callable(func), 'ComplexLambda: {} is not callable'.format(func) self.__func = func self.__args = List.wrap(a) self.__kwargs = kw
def __init__(self, f, stack, cause) -> None: self.f = f self.stack = List.wrap(stack) self.cause = cause
def with_index(self, fa: List[A]) -> List[Tuple[int, A]]: return List.wrap(enumerate(fa))
def flat_map(self, fa: List[A], f: Callable[[A], List[B]]) -> List[B]: return List.wrap(flatten(map(f, fa)))
def dispatch(self, obj, rpc_args): args = List.wrap(rpc_args).lift(0).get_or_else(List()) if self.check_length(args): return self._call_fun(obj, *args) else: return self.error(args)
def traverse(self): n = 3 l = LazyList(map(Just, range(n))) target = LazyList(List.wrap(range(n))) (l.sequence(Maybe) / _.drain).should.contain(target.drain)
def filter(self, fa: List[A], f: Callable[[A], bool]): return List.wrap(filter(f, fa))
def cmd_output(self, line: str) -> List[str]: return List.wrap(self.vim.command_output(line).split('\n'))
def __call__(self, *a, **kw): sub_a = self.__substitute(List.wrap(a)) return self.__func(*sub_a, **kw)
def content(self): return List.wrap(self.target[:]).map(decode)
def traverse(self): n = 3 target = Just(List.wrap(range(n))) List.wrap(map(Just, range(n))).sequence(Maybe).should.equal(target)
def cursor(self): return List.wrap(self.target.cursor)
def _log_out(self): return List.wrap(self.logfile.read_text().splitlines())
def cmd(self, cmdname, group, pat, *a, **kw): opts = List.wrap(a) + Map(kw).to_list.smap('{}={}'.format) c = 'syntax {} {} /{}/ {}'.format(cmdname, group, pat, ' '.join(opts)) self.target.cmd(c)
def _map(self, f: Callable): g = List.wrap(range(self.depth)).fold_left(f)(lambda z, i: __.map(z)) return g(self.value)
def map(self): l = LazyList(itertools.count(), chunk_size=20) l2 = l.map(_ * 10) l2[:5].should.equal(List.wrap(range(0, 50, 10)))