-
Notifications
You must be signed in to change notification settings - Fork 420
Open
Description
I propose a Dispatch method that build up many IEnumerable<T> from one IEnumerable<T>
IReadonlyList<IEnumerable<T>> Dispatch<T>(this IEnumerable<T> source, int portCount, Func<T, int> portSelector);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:
var nums = new [] {0,1,-1,2,-2,3,-3};
var ports = nums.Dispatch(v => v < 0 ? -1 : v%2); // discard negative number, put even number in port 0 and odd numbers in port 1ports[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:
(IEnumerable<T> First, IEnumerable<T> Second) Dispatch2<T>(this IEnumerable<T> source, Func<T, int> portSelector);
(IEnumerable<T> First, IEnumerable<T> Second, IEnumerable<T> Third) Dispatch3<T>(this IEnumerable<T> source, Func<T, int> portSelector);Metadata
Metadata
Assignees
Labels
No labels