-
Notifications
You must be signed in to change notification settings - Fork 415
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
Dispatch: group elements into ports/buckets by index #633
Comments
You can achieve this with var nums = new[] { 0, 1, -1, 2, -2, 3, -3 };
var (evens, odds, _) = // note that third set is computed but discarded
nums.GroupBy(v => v < 0 ? -1 : v % 2)
.Partition(0, 1, ValueTuple.Create); |
GroupBy will consume the full sequence on first iteration. A handcrafted implementation may avoid that. |
So
How's that any different? |
|
Perhaps something is missing in my understanding but I don't see how you can do this with sequences that are lazy. Also, your return value is a read-only list (of supposedly lazy sequence) and a list implies immediate execution semantics. |
I will try to build a useful example. Let says that you have the logs of a connection between a client and a server.
That is the Let's do it with my original proposition: var ports = input.Dispatch(s => s.StartWith("Q") ? 0 : s.StartWith("A") ? 1 : -1);
var results = ports[0].Zip(ports[1], (question, answer) => (question, answer)); With an input like "Q", "Q" ..., "Q", "A" where the first answer is at the position N, From this example we can improve the signature: IReadonlyDictionary<TKey, IEnumerable<TSource>> Dispatch<TKey, TSource>(
this IEnumerable<TSource> source,
IEnumerable<TKey> keys, // accepted keys, it's consumed immediately,
Func<TSource, TKey> keySelector);
// usage
var keys = new[] {'Q', 'A' };
var ports = input.Dispatch(keys, s => s[0]);
var results = ports['Q'].Zip(ports['A'], (question, answer) => (question, answer)); |
some overload of |
It's not full lazy, there are some caches.
|
I propose a
Dispatch
method that build up manyIEnumerable<T>
from oneIEnumerable<T>
For each element, the
portSelector
is evaluated and provide an index, the element is pushed onto port with the given index. We can use-1
as returned index to discard values.Exemple:
ports[0]
yield the sequence {0,2}ports[1]
yield the sequence {1,3}We can add overloads with predefined count of ports that return a tuple:
The text was updated successfully, but these errors were encountered: