Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async function composition #738

Closed
evhub opened this issue May 12, 2023 · 0 comments
Closed

Async function composition #738

evhub opened this issue May 12, 2023 · 0 comments

Comments

@evhub
Copy link
Owner

evhub commented May 12, 2023

There should be a way to write point-free async functions. What we need is some implementation of fmap and bind/join, treating either an awaitable itself or an async function returning an awaitable as the underlying monad. For example:

def and_then[**T, U, V](
	async_func: async (**T) -> U,
    normal_func: U -> V,
) -> async (**T) -> V =
    """Monadic fmap for async functions."""
    async def (*args, **kwargs) -> normal_func(await async_func(*args, **kwargs))

def then_await[**T, U](
    async_async_func: async (**T) -> Awaitable[U],
) -> async (**T) -> U =
    """Monadic join for async functions."""
    async def (*args, **kwargs) -> await (await async_async_func(*args, **kwargs))

def and_then_await[**T, U, V](
	async_f: async (**T) -> U,
    async_g: async U -> V,
) -> async (**T) -> V =
    """Monadic bind for async functions."""
    async_f `and_then` async_g `then_await`

which would let you do stuff like

my_async_func: async int -> int
new_async_func: async str -> int = (
    int
    ..> my_async_func
    `and_then`
    (.*2)
    `and_then_await`
    my_async_func
)
@evhub evhub added the feature label May 12, 2023
@evhub evhub changed the title Monadic/point-free async Async function composition Jun 9, 2023
@evhub evhub added the resolved label Jun 9, 2023
evhub added a commit that referenced this issue Jun 9, 2023
@evhub evhub closed this as completed Jun 9, 2023
@evhub evhub added this to the v3.0.3 milestone Jun 9, 2023
@evhub evhub mentioned this issue Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant