swift-async-algorithms icon indicating copy to clipboard operation
swift-async-algorithms copied to clipboard

Provide a higher arity version of merge(…)

Open tonyarnold opened this issue 3 years ago • 3 comments

I've got a use case where I have an unknown number of AsyncChannels that I want to merge into a single stream of events.

It'd be wonderful if merge(…) and friends supported more than an arity of 3 - ideally accepting a sequence?

Thanks!

tonyarnold avatar Oct 19 '22 10:10 tonyarnold

Hi

from what I know this is something that should arrive when generic variadics are available so we are not constrained to merging the same types of AsyncSequence.

Perhaps there are workarounds in your case. One that comes to my mind is (perhaps not the most efficient though):

Having a task group where you iterate over every channel you have (a task per channel) and send the elements to a single Channel. This “output” channel will act as a merging bus. You can then iterate over this single channel in another task.

twittemb avatar Oct 19 '22 10:10 twittemb

@twittemb Is right here that this is something we ought to be able to do one variadic generics land. My position right now is to avoid adding sequence based APIs since they have a few downsides such as introducing an array allocation which could be avoided.

What I can recommend you for the time being is either of these two:

  1. If merge is last transformation applied to your sequence chain then you can as @twittemb pointed out maybe use a task group and merge from there by just creating a child task for each upstream and calling next on the group repeatedly.
  2. You could copy the code that is used for the implementation of merge and just extend it to whatever arity you need. This involves very minor changes and a bit of code duplication.

FranzBusch avatar Oct 20 '22 12:10 FranzBusch

Thanks to you both - that guidance and info is much appreciated.

I'll see if I can work out how to extend the existing code to support different arities in my local project.

I'll also go read up on the parameter packs pitch by Holly and Slava - I'm really excited by this bit of work!

tonyarnold avatar Oct 24 '22 00:10 tonyarnold