Create filters without having a collection?

I think I may have a temporary mental glitch here, so excuse me if this is a stupid question, but I'm wondering if filters can be created so that they can be applied to a collection:

    static void Main(string[] args)
    {
        int[] a = { 1, 2, 3, 4 };
        Func<int, bool> filter = GetFilter();
        IEnumerable<int> result = a.Where(filter);
    }

    private static Func<int, bool> GetFilter()
    {
        Func<int, bool> filter = c => c % 2 == 0;

        // What if I wanted to further refine my filter here?
        // For instance, add a ceiling of 10

        return filter;
    }

      

+3


source to share


2 answers


I believe you need to add a reference to the original filter to avoid the recursive call.



var previousFilter = filter;
filter = c => previousFilter(c) && c <= 10;

      

+1


source


return f => c <= 10 && filter(c);

      



0


source







All Articles