maxfischer2781/asyncstdlib

Helper to emulate `async lambda`?

maxfischer2781 opened this issue · 3 comments

The ability to have an "async lambda" has occasionally been requested but so far not implemented in Python. This might be a chicken-and-egg problem, but it would certainly have been useful internally. While the new Py3.10 PEG Parser would make the syntax easy enough, the Python backend seems arcane to me and would not be backwards compatible.
Consider whether asyncstdlib could provide a backwards compatible helper to emulate async lambda.

Current alternatives/workarounds:

  • asyncstdlib.apply provides a means to combine a lambda with several awaitables.
  • a lambda and async generator can be combined to created an effective but obscure "async lambda":
    lambda x: (await x + 5 for _ in '_').__anext__()
    

This list will be updated as more arise.

Possible approaches:

  • helpers like apply for various practical cases
  • an "async monad" to combine operations
    • Likely very slow unless heavily invested in
    • Not "Pythonic", cumbersome for some things (e.g. boolean and/or)
  • a macro that rewrites source code
  • a helper that rewrites bytecode

Closing this for the time being. I'm not in favour of helpers for individual cases, since they don't actually cover the full lambda usecase. Metaprogramming via macros, bytecode or whatever seems way too heavy to maintain, and wouldn't play well with other tools.