dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.2k stars 1.57k forks source link

add asyncFold, asyncReduce to streams #18331

Open DartBot opened 10 years ago

DartBot commented 10 years ago

This issue was originally filed by @tatumizer


Continuing the line started by asyncMap, asyncExpand, please add other async primitives: asyncFold, asyncReduce etc. Using asyncFold, one can implement asynchronous loops without recursion.   

iposva-google commented 10 years ago

Added Area-Library, Library-Async, Triaged labels.

DartBot commented 10 years ago

This comment was originally written by @seaneagan


Are there any known use cases of needing to return Futures from Stream event handlers ?

Even if so, it still seems like in most cases instead of stream.asyncFold(initial, f) you could do stream.asyncMap(f1).fold(initial, f2) for some suitable f1 and f2.

I think a more common case of "asynchronous loops" is knowing the list of values up front. Quiver has doWhileAsync, reduceAsync and forEachAsync for this:

http://google.github.io/quiver-dart/#quiver/quiver-async

DartBot commented 10 years ago

This comment was originally written by @tatumizer


in quiver, there's a function Future reduceAsync <http://google.github.io/quiver-dart/#quiver/quiver-async@id_reduceAsync>\( Iterable <http://google.github.io/quiver-dart/#dart-core.Iterable> iterable<http://google.github.io/quiver-dart/#quiver/quiver-async@id_reduceAsync,iterable>, initialValue<http://google.github.io/quiver-dart/#quiver/quiver-async@id_reduceAsync,initialValue>, AsyncCombiner<http://google.github.io/quiver-dart/#quiver/quiver-async.AsyncCombiner> combine<http://google.github.io/quiver-dart/#quiver/quiver-async@id_reduceAsync,combine> ) Which means you know the use case for async reduce over Iterable. Take this iterable and send it somewhere, element by element, as a stream. You get use case in question :-)

lrhn commented 10 years ago

Removed Type-Defect label. Added Type-Enhancement label.