adds missing mapFuture operation to SafeAsyncIter

This commit is contained in:
Marcin Czenko 2025-06-17 02:27:48 +02:00
parent 4b9d967b93
commit b158d43729
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0

View File

@ -34,6 +34,7 @@ import ./iter
## - next - to get the next item from the async iterator
## - items - to iterate over the async iterator
## - pairs - to iterate over the async iterator and return the index of each item
## - mapFuture - to convert a (raising) Future[T] to a (raising) Future[U] using a function fn: auto -> Future[U] - we use auto to handle both raising and non-raising futures
## - mapAsync - to convert a regular sync iterator (Iter) to an async iter (SafeAsyncIter)
## - map - to convert one async iterator (SafeAsyncIter) to another async iter (SafeAsyncIter)
## - mapFilter - to convert one async iterator (SafeAsyncIter) to another async iter (SafeAsyncIter) and apply filtering at the same time
@ -150,6 +151,12 @@ iterator pairs*[T](self: SafeAsyncIter[T]): auto {.inline.} =
yield (i, self.next())
inc(i)
proc mapFuture*[T, U](
fut: auto, fn: SafeFunction[T, U]
): Future[U] {.async: (raises: [CancelledError]).} =
let t = await fut
await fn(t)
proc mapAsync*[T, U](
iter: Iter[T], fn: SafeFunction[T, ?!U], finishOnErr: bool = true
): SafeAsyncIter[U] =