Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge method for IAsyncEnumerable #1064

Closed
Daniel15 opened this issue Oct 29, 2019 · 6 comments

Comments

@Daniel15
Copy link

@Daniel15 Daniel15 commented Oct 29, 2019

Is there a "merge" method for IAsyncEnumerable that will combine multiple IAsyncEnumerables into a single IAsyncEnumerable that yields items in the order they're received? I thought something like this would be in System.Linq.Async, but I don't see anything for it.

@akarnokd

This comment has been minimized.

Copy link
Collaborator

@akarnokd akarnokd commented Oct 29, 2019

Do you mean running multiple sources at once but still getting items in order as with Concat?

There is a ConcatEager operator in my extensions project.

@Daniel15

This comment has been minimized.

Copy link
Author

@Daniel15 Daniel15 commented Oct 29, 2019

Say I have two IAsyncEnumerables, a and b, that produce items like this, at the specified times:

a:

  • 00:00: hello
  • 00:05: world

b:

  • 00:01: foo
  • 00:02: bar
  • 00:06: baz

I want to get a single enumerable that produces items in this order:

  • hello
  • foo
  • bar
  • world
  • baz

I'll take a look at your project; maybe it does what I need.

@akarnokd

This comment has been minimized.

Copy link
Collaborator

@akarnokd akarnokd commented Oct 29, 2019

That looks like plain AsyncEnumerableEx.Merge.

@Daniel15

This comment has been minimized.

Copy link
Author

@Daniel15 Daniel15 commented Oct 30, 2019

@akarnokd - Works great, thanks!

public static async Task Main(string[] args)
{
	var sources = new[]
	{
		A(),
		B(),
	};

	await foreach (var item in async_enumerable_dotnet.AsyncEnumerable.Merge(sources))
	{
		Console.WriteLine(item);
	}
}

private static async IAsyncEnumerable<string> A()
{
	yield return "hello";
	await Task.Delay(5000);
	yield return "world";
}

private static async IAsyncEnumerable<string> B()
{
	await Task.Delay(1000);
	yield return "foo";
	await Task.Delay(1000);
	yield return "bar";
	await Task.Delay(4000);
	yield return "baz";
}

Output:

hello
foo
bar
world
baz

Do you think that .Merge method will ever be merged into System.Linq.Async? It's very useful, and I imagine will be a pretty common operation.

@akarnokd

This comment has been minimized.

Copy link
Collaborator

@akarnokd akarnokd commented Oct 30, 2019

There is an official one with different internals:

public static IAsyncEnumerable<TSource> Merge<TSource>(params IAsyncEnumerable<TSource>[] sources)

@Daniel15

This comment has been minimized.

Copy link
Author

@Daniel15 Daniel15 commented Oct 31, 2019

Oops, I missed that as it's in the System.Interactive.Async assembly and I only had System.Linq.Async installed 😅

@Daniel15 Daniel15 closed this Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.