|
| 1 | +"""AsyncOption builder module. |
| 2 | +
|
| 3 | +The AsyncOption builder allows for composing asynchronous operations that |
| 4 | +may return an optional value, using the Option type. It's similar to the Option builder but |
| 5 | +works with async operations. |
| 6 | +""" |
| 7 | + |
| 8 | +from collections.abc import AsyncGenerator, Awaitable, Callable |
| 9 | +from typing import Any, ParamSpec, TypeVar |
| 10 | + |
| 11 | +from expression.core import Nothing, Option, Some |
| 12 | +from expression.core.async_builder import AsyncBuilder |
| 13 | + |
| 14 | + |
| 15 | +_TSource = TypeVar("_TSource") |
| 16 | +_TResult = TypeVar("_TResult") |
| 17 | +_P = ParamSpec("_P") |
| 18 | + |
| 19 | + |
| 20 | +class AsyncOptionBuilder(AsyncBuilder[_TSource, Option[Any]]): |
| 21 | + """AsyncOption builder. |
| 22 | +
|
| 23 | + The AsyncOption builder allows for composing asynchronous operations that |
| 24 | + may return an optional value, using the Option type. |
| 25 | + """ |
| 26 | + |
| 27 | + async def bind( |
| 28 | + self, |
| 29 | + xs: Option[_TSource], |
| 30 | + fn: Callable[[Any], Awaitable[Option[_TResult]]], |
| 31 | + ) -> Option[_TResult]: |
| 32 | + """Bind a function to an async option value. |
| 33 | +
|
| 34 | + In F# computation expressions, this corresponds to ``let!`` and enables |
| 35 | + sequencing of computations. |
| 36 | +
|
| 37 | + Args: |
| 38 | + xs: The async option value to bind |
| 39 | + fn: The function to apply to the value if Some |
| 40 | +
|
| 41 | + Returns: |
| 42 | + The result of applying fn to the value if Some, otherwise Nothing |
| 43 | + """ |
| 44 | + match xs: |
| 45 | + case Option(tag="some", some=value): |
| 46 | + return await fn(value) |
| 47 | + case _: |
| 48 | + return Nothing |
| 49 | + |
| 50 | + async def return_(self, x: _TSource) -> Option[_TSource]: |
| 51 | + """Wrap a value in an async option. |
| 52 | +
|
| 53 | + In F# computation expressions, this corresponds to ``return`` and lifts |
| 54 | + a value into the option context. |
| 55 | +
|
| 56 | + Args: |
| 57 | + x: The value to wrap |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Some containing the value |
| 61 | + """ |
| 62 | + return Some(x) |
| 63 | + |
| 64 | + async def return_from(self, xs: Option[_TSource]) -> Option[_TSource]: |
| 65 | + """Return an async option value directly. |
| 66 | +
|
| 67 | + In F# computation expressions, this corresponds to ``return!`` and allows |
| 68 | + returning an already wrapped value. |
| 69 | +
|
| 70 | + Args: |
| 71 | + xs: The async option value to return |
| 72 | +
|
| 73 | + Returns: |
| 74 | + The async option value unchanged |
| 75 | + """ |
| 76 | + return xs |
| 77 | + |
| 78 | + async def combine(self, xs: Option[_TSource], ys: Option[_TSource]) -> Option[_TSource]: |
| 79 | + """Combine two async option computations. |
| 80 | +
|
| 81 | + In F# computation expressions, this enables sequencing multiple |
| 82 | + expressions where we only care about the final result. |
| 83 | +
|
| 84 | + Args: |
| 85 | + xs: First async option computation |
| 86 | + ys: Second async option computation |
| 87 | +
|
| 88 | + Returns: |
| 89 | + The second computation if first is Some, otherwise Nothing |
| 90 | + """ |
| 91 | + match xs: |
| 92 | + case Option(tag="some"): |
| 93 | + return ys |
| 94 | + case _: |
| 95 | + return Nothing |
| 96 | + |
| 97 | + async def zero(self) -> Option[Any]: |
| 98 | + """Return the zero value for async options. |
| 99 | +
|
| 100 | + In F# computation expressions, this is used when no value is returned, |
| 101 | + corresponding to None in F#. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + Nothing |
| 105 | + """ |
| 106 | + return Nothing |
| 107 | + |
| 108 | + async def delay(self, fn: Callable[[], Option[_TSource]]) -> Option[_TSource]: |
| 109 | + """Delay the computation. |
| 110 | +
|
| 111 | + Default implementation is to return the result of the function. |
| 112 | + """ |
| 113 | + return fn() |
| 114 | + |
| 115 | + async def run(self, computation: Option[_TSource]) -> Option[_TSource]: |
| 116 | + """Run a computation. |
| 117 | +
|
| 118 | + Default implementation is to return the computation as is. |
| 119 | + """ |
| 120 | + return computation |
| 121 | + |
| 122 | + def __call__( |
| 123 | + self, |
| 124 | + fn: Callable[ |
| 125 | + _P, |
| 126 | + AsyncGenerator[_TSource, _TSource] | AsyncGenerator[_TSource, None], |
| 127 | + ], |
| 128 | + ) -> Callable[_P, Awaitable[Option[_TSource]]]: |
| 129 | + """The builder decorator.""" |
| 130 | + return super().__call__(fn) |
| 131 | + |
| 132 | + |
| 133 | +# Create singleton instance |
| 134 | +async_option: AsyncOptionBuilder[Any] = AsyncOptionBuilder() |
| 135 | + |
| 136 | + |
| 137 | +__all__ = ["AsyncOptionBuilder", "async_option"] |
0 commit comments