async def get_value(n: Notification[Any]) -> Option[Any]: with match(n) as m: for value in case(OnNext[TSource]): return Some(value) for err in case(OnError): await safe_obv.athrow(err) while m.default(): await safe_obv.aclose() return Nothing
def add(k: Key, v: Value, m: MapTree[Key, Value]) -> MapTree[Key, Value]: for m2 in m.to_list(): if isinstance(m2, MapTreeNode): mn = cast(MapTreeNode[Key, Value], m2) if k < mn.key: return rebalance(add(k, v, mn.left), mn.key, mn.value, mn.right) elif k == mn.key: return Some(MapTreeNode(k, v, mn.left, mn.right, mn.height)) else: return rebalance(mn.left, mn.key, mn.value, add(k, v, mn.right)) else: if k < m2.key: return Some(MapTreeNode(k, v, empty, m, 2)) elif k == m2: return Some(MapTreeLeaf(k, v)) else: return Some(MapTreeNode(k, v, m, empty, 2)) else: return Some(MapTreeLeaf(k, v))
def try_find(k: Key, m: MapTree[Key, Value]) -> Option[Value]: for m2 in m.to_list(): if k == m2.key: return Some(m2.value) else: if isinstance(m2, MapTreeNode): mn = cast(MapTreeNode[Key, Value], m2) return try_find(k, mn.left if k < m2 else mn.right) else: return Nothing else: # Nothing return Nothing
def test_option_builder_yield_some_wrapped(): @effect.option def fn() -> Generator[Option[int], Option[int], Option[int]]: x = yield Some(42) return x xs = fn() for value in xs.match(Some): assert value == Some(42) break else: assert False
def test_option_builder_yield_some_wrapped(): @effect.option def fn() -> Generator[Option[int], Option[int], Option[int]]: x = yield Some(42) return x xs = fn() assert Some(42) == xs.match( Some, lambda some: some.value, _, None, )
def collapseLHS( stack: FrozenList[MapTree[Key, Value]]) -> FrozenList[MapTree[Key, Value]]: if stack.is_empty(): return frozenlist.empty m, rest = stack.head(), stack.tail() for m2 in m.to_list(): if isinstance(m2, MapTreeNode): mn = cast(MapTreeNode[Key, Value], m2) tree = Some(MapTreeLeaf(mn.key, mn.value)) return collapseLHS(rest.cons(mn.right).cons(tree).cons(mn.left)) else: return stack else: return collapseLHS(rest)
def matcher(value: str) -> Option[str]: with match(value) as case: while case("rxpy"): assert False for value in case(str): assert value == "expression" return Some(value) for value in case("aioreactive"): assert False if case._: assert False return Nothing
def change(k: Key, u: Callable[[Option[Value]], Option[Value]], m: MapTree[Key, Value]) -> MapTree[Key, Value]: for m2 in m.to_list(): if isinstance(m2, MapTreeNode): mn = cast(MapTreeNode[Key, Value], m2) if k < mn.key: rebalance(change(k, u, mn.left), mn.key, mn.value, mn.right) elif k == mn.key: for v in u(Some(mn.value)).to_list(): return Some(MapTreeNode(k, v, mn.left, mn.right, mn.height)) else: if is_empty(mn.left): return mn.right elif is_empty(mn.right): return mn.left else: sk, sv, r_ = splice_out_successor(mn.right) return mk(mn.left, sk, sv, r_) else: rebalance(mn.left, mn.key, mn.value, change(k, u, mn.right)) else: if k < m2.key: for v in u(Nothing).to_list(): return Some(MapTreeNode(k, v, empty, m, 2)) else: return m elif k == m2.key: for v in u(Some(m2.value)).to_list(): return Some(MapTreeLeaf(k, v)) else: return empty else: for v in u(Nothing).to_list(): return Some(MapTreeNode(k, v, m, empty, 2)) else: return m else: for v in u(Nothing): return Some(MapTreeLeaf(k, v)) else: return m
async def fetch( next: HttpFunc[Option[ClientResponse], TResult, TError], ctx: HttpContext, ) -> Result[Context[TResult], TError]: session = ctx.Request.SessionFactory() builder: Callable[[Any], HttpContent] = lambda builder: builder() result: HttpFuncResult try: content: Option[Any] = pipe(ctx.Request.ContentBuilder, option.map(builder)) json = pipe(content, option.default_value(None)) method = ctx.Request.Method.value url = ctx.Request.UrlBuilder(ctx) print(f"Fetching: {url}") async with session.request(method=method, url=url, json=json) as resp: result = await next(ctx.replace(Response=Some(resp))) except Exception as ex: print(f"fetch: {ex}") result = Error(ex) return result
def fn() -> Generator[Option[int], Option[int], Option[int]]: x = yield Some(42) return x
def test_option_some_map_fluent(): xs = Some(42) ys = xs.map(lambda x: x + 1) assert ys.match(Some, lambda some: some.value == 43, _, False)
def test_option_some(): xs = Some(42) assert isinstance(xs, Option) assert pipe(xs, option.is_some) is True assert pipe(xs, option.is_none) is False
def unfolder(state: int) -> Option[Tuple[int, int]]: if state < x: return Some((state, state + 1)) return Nothing
def test_option_some_to_list(): xs = Some(42) assert xs.to_list() == [42]
def test_option_none_not_equals_some(): xs = Some(42) ys = Nothing assert xs != ys assert ys != xs
def test_option_some_equals_some(a: Any, b: Any): xs = Some(a) ys = Some(b) assert xs == ys if a == b else xs != ys
def fn() -> Generator[int, int, int]: x = yield from Nothing y = yield from Some(43) return x + y
def test_pipeline_none(): hn = pipeline() assert hn(42) == Some(42)
def test_option_some_map2_piped(x: int, y: int): xs = Some(x) ys = Some(y) zs = pipe2((xs, ys), option.map2(lambda x, y: x + y)) assert zs.match(Some, lambda some: some.value, _, False) == x + y
def test_option_some_to_seq(): xs = Some(42) assert list(xs.to_seq()) == [42]
def test_option_some_to_str(): xs = Some(42) assert str(xs) == f"Some {xs.value}"
def test_option_some_is_some(): xs = Some(42) assert xs.is_some()
def test_option_some_is_none(): xs = Some(42) assert not xs.is_none()
def return_(self, x: TSource) -> Option[TSource]: return Some(x)
def fn() -> Generator[int, int, int]: x = yield from Some(42) return x + 1