Skip to content

feat: support async callbacks in async sequence operations - #312

Draft
dbrattli wants to merge 2 commits into
mainfrom
feat/asyncseq-amap
Draft

dbrattli wants to merge 2 commits into
mainfrom
feat/asyncseq-amap

Conversation

@dbrattli

@dbrattli dbrattli commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Async sequence operations currently accept synchronous callbacks: passing an async mapper yields coroutine objects instead of resolved values. Add explicit awaitable-callback operations in both the pipe-friendly module API and fluent AsyncSeq API:

  • amap, afilter, achoose, and acollect
  • ascan, afold, and afold_back

All accept ordinary and async sources and await callbacks sequentially. Stream transformations are synchronous factories returning lazy async iterables (or AsyncSeq for fluent calls), so ordinary pipe composes them. Folds return coroutines; use await for a final result or apipe when subsequent stages need that result.

stream = pipe([1, 2, 3], asyncseq.amap(enrich_inputs), asyncseq.afilter(is_allowed))
items = [item async for item in stream]

stream = AsyncSeq.range(3).amap(enrich_inputs).afilter(is_allowed)
total = await stream.afold(add, 0)

amap preserves one output per input. acollect awaits each callback and consumes its ordinary or async inner iterable before advancing the source. ascan yields the initial state first. Both folds return the initial state for empty input; afold_back buffers the finite source and invokes (item, state) callbacks in reverse order, requiring linear memory. Other state callbacks receive (state, item). Exceptions and cancellation propagate, with no background tasks or concurrent callback execution.

Includes documentation and tests for ordinary/async sources, fluent/functional APIs, empty input, nested results, general awaitables, type inference, lazy sequential execution, fold order, failures, and cancellation. Concurrent mapping (#227) remains separate.

Closes #213.

Validation:

  • .venv/bin/pytest — 729 passed.
  • .venv/bin/python scripts/check_docs.py — 17 documentation examples passed.
  • pre-commit run --all-files --show-diff-on-failure — all hooks passed, including Ruff and strict Pyright/negative typing contracts.

@dbrattli dbrattli changed the title feat: add sequential async sequence mapping feat: support async callbacks in async sequence operations Nov 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async methods

1 participant