Beispiel #1
0
 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)))
Beispiel #2
0
 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
Beispiel #3
0
    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
Beispiel #4
0
 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
Beispiel #5
0
 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)
Beispiel #6
0
 def _flat_map(self, f: Callable):
     ''' **f** must return the same stack type as **self.value** has.
     Iterates over the effects, sequences the inner instance
     successively to the top and joins with the outer instance.
     Example:
     List(Right(Just(1))) => List(Right(Just(List(Right(Just(5))))))
     => List(List(Right(Just(Right(Just(5))))))
     => List(Right(Just(Right(Just(5)))))
     => List(Right(Right(Just(Just(5)))))
     => List(Right(Just(Just(5))))
     => List(Right(Just(5)))
     Note: Task works only as outermost effect, as it cannot sequence
     '''
     index = List.range(self.depth + 1)
     g = index.fold_left(f)(lambda z, i: __.map(z))
     nested = g(self.value)
     def sequence_level(z, depth, tpe):
         nesting = lambda z, i: __.map(z).sequence(tpe)
         lifter = List.range(depth).fold_left(I)(nesting)
         return z // lifter
     def sequence_type(z, data):
         return L(sequence_level)(_, *data).map(z)
     h = self.all_effects.reversed.with_index.fold_left(I)(sequence_type)
     return h(nested)
Beispiel #7
0
 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)
Beispiel #8
0
 def deep(self):
     n = int(1e4)
     l = LazyList(List.wrap(range(n)))
     l.index_of(n - 1).should.contain(n - 1)
Beispiel #9
0
 def set(self, value):
     super().set(value)
     glob = lambda a: a.parent.glob(a.name)
     self.value = List.wrap(self.value) / _.value // glob
Beispiel #10
0
 def _auto_handlers(self, cls):
     import inspect
     return List.wrap(inspect.getmembers(cls)).flat_map2(self._auto_handler)
Beispiel #11
0
 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
Beispiel #12
0
 def __init__(self, f, stack, cause) -> None:
     self.f = f
     self.stack = List.wrap(stack)
     self.cause = cause
Beispiel #13
0
 def with_index(self, fa: List[A]) -> List[Tuple[int, A]]:
     return List.wrap(enumerate(fa))
Beispiel #14
0
 def flat_map(self, fa: List[A], f: Callable[[A], List[B]]) -> List[B]:
     return List.wrap(flatten(map(f, fa)))
Beispiel #15
0
 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)
Beispiel #16
0
 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)
Beispiel #17
0
 def filter(self, fa: List[A], f: Callable[[A], bool]):
     return List.wrap(filter(f, fa))
Beispiel #18
0
 def cmd_output(self, line: str) -> List[str]:
     return List.wrap(self.vim.command_output(line).split('\n'))
Beispiel #19
0
 def __call__(self, *a, **kw):
     sub_a = self.__substitute(List.wrap(a))
     return self.__func(*sub_a, **kw)
Beispiel #20
0
 def content(self):
     return List.wrap(self.target[:]).map(decode)
Beispiel #21
0
 def traverse(self):
     n = 3
     target = Just(List.wrap(range(n)))
     List.wrap(map(Just, range(n))).sequence(Maybe).should.equal(target)
Beispiel #22
0
 def cursor(self):
     return List.wrap(self.target.cursor)
Beispiel #23
0
 def _log_out(self):
     return List.wrap(self.logfile.read_text().splitlines())
Beispiel #24
0
 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)
Beispiel #25
0
 def _map(self, f: Callable):
     g = List.wrap(range(self.depth)).fold_left(f)(lambda z, i: __.map(z))
     return g(self.value)
Beispiel #26
0
def decode_list(value):
    from tryp import List
    return List.wrap(value).map(decode)
Beispiel #27
0
 def sequence_level(z, depth, tpe):
     nesting = lambda z, i: __.map(z).sequence(tpe)
     lifter = List.range(depth).fold_left(I)(nesting)
     return z // lifter
Beispiel #28
0
 def map(self):
     l = LazyList(itertools.count(), chunk_size=20)
     l2 = l.map(_ * 10)
     l2[:5].should.equal(List.wrap(range(0, 50, 10)))