Skip to content

Dispatch: group elements into ports/buckets by index #633

@Orace

Description

@Orace

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 1
  • 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:

(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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions