lundi 16 juillet 2018

Returning many lists from a function

Suppose I have a function which populates a few lists like the following:

public static List<T> foo()
{
    List<T> list1 = List<T>();
    List<T> list2 = List<T>();
    List<T> list3 = List<T>();
    foreach (T item in someList)  # Cannot change
    {
        if (condition1)
        {
            list1.add(item);
        }
        if (condition2)
        {
            list2.add(item);
        }
        if (condition3)
        {
            list3.add(item);
        }
    }
    return list1;
}

At the moment I only return one list, however I'm interested in returning all the lists since each one contains elements which must be handled differently by the calling function. What should I be returning here? I've considered a tuple of lists, or a list of tuples, but I would expect that there are other designs which would serve this pattern better.

I can't separate the iteration into separate functions for each list, the actual iteration calls a command which isn't idempotent.

Aucun commentaire:

Enregistrer un commentaire