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)
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 _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, f: Callable): return self.copy(self._map(__.map(f)))
from typing import TypeVar, Callable from lenses import Lens, lens from tryp import Maybe, List, __, Boolean, _, L A = TypeVar('A') _add = lambda l: __.map(lens().add_lens(l).add_lens) def path_lens_pred(a: A, sub: Callable[[A], List[A]], lsub, f: Callable[[A], bool]): g = lambda a: Boolean(f(lsub(a))).maybe(lsub(lens())) return path_lens(a, sub, g) def path_lens(a: A, sub: Callable[[A], List[A]], f: Callable[[A], Maybe[Lens]]) -> Maybe[Lens]: return _path_lens(a, sub, f).smap(lens(a).tuple_) def path_lens_unbound_pre(a: A, sub: Callable[[A], List[A]], f: Callable[[A], Maybe[Lens]], pre: Callable): return ( _path_lens(pre(a), sub, f) / (_ / pre(lens()).add_lens) / __.cons(lens()) ).smap(lens().tuple_)