ExtremeAndy.CombinatoryFilters 1.1.8

Functional filter abstraction for creating, applying, mapping, and reducing combinatory filter structures

Install-Package ExtremeAndy.CombinatoryFilters -Version 1.1.8
dotnet add package ExtremeAndy.CombinatoryFilters --version 1.1.8
<PackageReference Include="ExtremeAndy.CombinatoryFilters" Version="1.1.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExtremeAndy.CombinatoryFilters --version 1.1.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

CombinatoryFilters

Functional filter abstraction for creating, applying, mapping, and reducing combinatory filter structures

Installation

dotnet add package ExtremeAndy.CombinatoryFilters

Usage

  1. Define your filter interface(s) and/or class(es). Here's an example of a simple filter which checks whether an integer is between UpperBound and LowerBound.
public class NumericRangeFilter : LeafFilterNode<NumericRangeFilter>, IRealisableLeafFilterNode<int>
{
    public NumericRangeFilter(int lowerBound, int upperBound)
    {
        LowerBound = lowerBound;
        UpperBound = upperBound;
    }

    public int LowerBound { get; }

    public int UpperBound { get; }

    public bool IsMatch(int item) => LowerBound <= item && item <= UpperBound;
}
  1. Optionally implement IEquatable&lt;IFilterNode&gt; on your filter class. If this is not done, then calling .Equals() on an IFilterNode in your filter tree will default to reference equality when comparing your leaf filters.

  2. Create an instance of your filter and apply it to some values

var filter = new NumericRangeFilter(5, 10);
var values = new[] { 1, 3, 5, 9, 11 };
var expectedFilteredValues = new[] { 5, 9 };

var filterPredicate = filter.GetPredicate<NumericRangeFilter, int>();
var filteredValues = values.Where(filterPredicate);

Assert.Equal(expectedFilteredValues, filteredValues);

Complex filters

You can assemble arbitrarily complex filters as follows:

var filter5To10 = new NumericRangeFilter(5, 10);
var filter8To15 = new NumericRangeFilter(8, 15);
var filter5To10Or8To15 = new CombinationFilter<NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
var filter9To12 = new NumericRangeFilter(9, 12);
var filter = new CombinationFilter<NumericRangeFilter>(new IFilterNode<NumericRangeFilter>[] { filter5To10Or8To15, filter9To12 }, CombinationOperator.All);
Inversion

Any filter can be inverted using .Invert().

Testing a single value

You can test a single value as follows:

var filter = new NumericRangeFilter(5, 10);
var isMatch = filter.IsMatch(7);

Preserving ordering of filters

CombinationFilter stores filters as an IImmutableSet. If you wish to preserve the order of your filters, use OrderedCombinationFilter instead.

Advanced usage

IFilterNode&lt;&gt; supports Map, Match and Aggregate for mapping and reducing filters.

Map usage

In this example, we reduce the range of the leaf node filters by increasing the lower bound by 1 and decreasing the upper bound by 1. The structure of all the All, Any and Invert operations remains unchanged.

var shortenedFilters = filter.Map(f =>
{
    var newLowerBound = f.LowerBound + 1;
    var newUpperBound = f.UpperBound - 1;
    return new NumericRangeFilter(newLowerBound, newUpperBound);
});

Aggregate usage

In this example, we want to compute the length of the longest filter interval, or infinity if any filter is inverted.

var longestIntervalLength = filter.Aggregate<double>(
    (lengths, _) => lengths.Max(),
    length => double.PositiveInfinity,
    f => f.UpperBound - f.LowerBound);

GetPartial usage

GetPartial provides a way to compute a partial filter, which is a kind of subset of a filter. When applied, a partial filter is guaranteed to return a superset of the result that the original filter would have returned when applied.

This is useful for performing pre-filtering on an incomplete dataset that doesn't (yet) contain all the information required to apply the final filter.

This is normally quite a trivial problem, but when there are InvertedFilters and CombinationFilters in the mix, computing the minimal partial filter is not intuitive or easy to demonstrate.

Here is a contrived example (note: this doesn't do anything useful, just demonstrates usage):

// All the numbers from -5 to 10, excluding numbers from 2 to 6
var filter = new CombinationFilter<NumericRangeFilter>(new IFilterNode<NumericRangeFilter>[]
{
    new NumericRangeFilter(-5, 10),
    new InvertedFilter<NumericRangeFilter>(new NumericRangeFilter(2, 6)), 
}, CombinationOperator.All);

// Exclude filters with negative values
var partialFilter = filter.GetPartial(f => f.LowerBound >= 0);

// Initially we only have positive numbers
var positiveValues = new[] {1, 3, 5, 7, 12};
var prefilteredValues = positiveValues.Where(partialFilter.IsMatch).ToList();
Assert.Equal(new[] { 1, 7, 12 }, prefilteredValues);

// Now we include some additional values
var additionalValues = new[] { -7, -4, 11 };
var combinedValues = prefilteredValues.Concat(additionalValues);

// Finally we apply our 'full' filter
var finalValues = combinedValues.Where(filter.IsMatch);
Assert.Equal(new[] { 1, 7, -4 }, finalValues);

CombinatoryFilters

Functional filter abstraction for creating, applying, mapping, and reducing combinatory filter structures

Installation

dotnet add package ExtremeAndy.CombinatoryFilters

Usage

  1. Define your filter interface(s) and/or class(es). Here's an example of a simple filter which checks whether an integer is between UpperBound and LowerBound.
public class NumericRangeFilter : LeafFilterNode<NumericRangeFilter>, IRealisableLeafFilterNode<int>
{
    public NumericRangeFilter(int lowerBound, int upperBound)
    {
        LowerBound = lowerBound;
        UpperBound = upperBound;
    }

    public int LowerBound { get; }

    public int UpperBound { get; }

    public bool IsMatch(int item) => LowerBound <= item && item <= UpperBound;
}
  1. Optionally implement IEquatable&lt;IFilterNode&gt; on your filter class. If this is not done, then calling .Equals() on an IFilterNode in your filter tree will default to reference equality when comparing your leaf filters.

  2. Create an instance of your filter and apply it to some values

var filter = new NumericRangeFilter(5, 10);
var values = new[] { 1, 3, 5, 9, 11 };
var expectedFilteredValues = new[] { 5, 9 };

var filterPredicate = filter.GetPredicate<NumericRangeFilter, int>();
var filteredValues = values.Where(filterPredicate);

Assert.Equal(expectedFilteredValues, filteredValues);

Complex filters

You can assemble arbitrarily complex filters as follows:

var filter5To10 = new NumericRangeFilter(5, 10);
var filter8To15 = new NumericRangeFilter(8, 15);
var filter5To10Or8To15 = new CombinationFilter<NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
var filter9To12 = new NumericRangeFilter(9, 12);
var filter = new CombinationFilter<NumericRangeFilter>(new IFilterNode<NumericRangeFilter>[] { filter5To10Or8To15, filter9To12 }, CombinationOperator.All);
Inversion

Any filter can be inverted using .Invert().

Testing a single value

You can test a single value as follows:

var filter = new NumericRangeFilter(5, 10);
var isMatch = filter.IsMatch(7);

Preserving ordering of filters

CombinationFilter stores filters as an IImmutableSet. If you wish to preserve the order of your filters, use OrderedCombinationFilter instead.

Advanced usage

IFilterNode&lt;&gt; supports Map, Match and Aggregate for mapping and reducing filters.

Map usage

In this example, we reduce the range of the leaf node filters by increasing the lower bound by 1 and decreasing the upper bound by 1. The structure of all the All, Any and Invert operations remains unchanged.

var shortenedFilters = filter.Map(f =>
{
    var newLowerBound = f.LowerBound + 1;
    var newUpperBound = f.UpperBound - 1;
    return new NumericRangeFilter(newLowerBound, newUpperBound);
});

Aggregate usage

In this example, we want to compute the length of the longest filter interval, or infinity if any filter is inverted.

var longestIntervalLength = filter.Aggregate<double>(
    (lengths, _) => lengths.Max(),
    length => double.PositiveInfinity,
    f => f.UpperBound - f.LowerBound);

GetPartial usage

GetPartial provides a way to compute a partial filter, which is a kind of subset of a filter. When applied, a partial filter is guaranteed to return a superset of the result that the original filter would have returned when applied.

This is useful for performing pre-filtering on an incomplete dataset that doesn't (yet) contain all the information required to apply the final filter.

This is normally quite a trivial problem, but when there are InvertedFilters and CombinationFilters in the mix, computing the minimal partial filter is not intuitive or easy to demonstrate.

Here is a contrived example (note: this doesn't do anything useful, just demonstrates usage):

// All the numbers from -5 to 10, excluding numbers from 2 to 6
var filter = new CombinationFilter<NumericRangeFilter>(new IFilterNode<NumericRangeFilter>[]
{
    new NumericRangeFilter(-5, 10),
    new InvertedFilter<NumericRangeFilter>(new NumericRangeFilter(2, 6)), 
}, CombinationOperator.All);

// Exclude filters with negative values
var partialFilter = filter.GetPartial(f => f.LowerBound >= 0);

// Initially we only have positive numbers
var positiveValues = new[] {1, 3, 5, 7, 12};
var prefilteredValues = positiveValues.Where(partialFilter.IsMatch).ToList();
Assert.Equal(new[] { 1, 7, 12 }, prefilteredValues);

// Now we include some additional values
var additionalValues = new[] { -7, -4, 11 };
var combinedValues = prefilteredValues.Concat(additionalValues);

// Finally we apply our 'full' filter
var finalValues = combinedValues.Where(filter.IsMatch);
Assert.Equal(new[] { 1, 7, -4 }, finalValues);

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.8 314 8/4/2019
1.1.7 54 8/4/2019
1.1.6 60 8/4/2019
1.1.5 57 8/2/2019
1.1.4 58 8/2/2019
1.1.3 57 8/2/2019
1.1.2 57 8/2/2019
1.1.1 57 8/2/2019
1.1.0 58 8/2/2019
1.0.4 99 7/22/2019
1.0.3 53 7/21/2019
1.0.2 52 7/21/2019
1.0.1 53 7/20/2019
1.0.0 53 7/20/2019