def _min_max(self, fa: F[A], f: Callable[[A], int], pred: Callable[[int, int], int]) -> Maybe[int]: def folder(z: Maybe[A], a: A) -> Maybe[int]: return ( z.map(lambda b: b if pred(f(b), f(a)) else a) .or_else(Just(a)) ) return self.fold_left(fa, Empty())(folder)
def _drain_find(self, abort: Callable[[A], bool]) -> Maybe[A]: culprit = Empty() def gen() -> Generator: nonlocal culprit while True: try: el = next(self.source) yield el if abort(el): culprit = Just(el) break except StopIteration: break drained = List.wrap(list(gen())) self.strict = self.strict + drained return culprit
def to_maybe(self, fa: Either[A, B]) -> Maybe[A]: return Just(fa.value) if fa.is_right else Empty()