Aggregate values until a limit is reached

给你一囗甜甜゛ 提交于 2020-01-02 05:18:09

问题


I need something similar to an AggregateWhile method. The standard System.Linq.Enumerable class doesn't provide it. Until now I've always been able to leverage the standard LINQ methods to solve every problem I've encountered. So I'd like to know if that's still possible in this case, or if I really do need to extend LINQ with a non-standard method.

The hypothetical AggregateWhile method would iterate over a sequence and apply the accumulator. The aggregation would be complete once a predicate returns false. The result is the aggregration of elements up to but not including the element for which the predicate failed.

Here's an example. We have a List { 1, 2, 3, 4, 5 } with an accumulator that adds the two input numbers together, and a predicate that states the accumulation must be less than 12. AggregateWhile would return 10 since that's the result of 1 + 2 + 3 + 4 and adding the final 5 would push the total over the limit. In code:

var list = new List<int> { 1, 2, 3, 4, 5 };
int total = list.AggregateWhile( (x, y) => x + y, a => a < 12 ); // returns 10

I need a purely functional solution, so closing over a temporary variable is not an option.


回答1:


You could either write the function yourself, or carry a flag with your accumulator:

int total = list.Aggregate(new { value = 0, valid = true }, 
                          (acc, v) => acc.value + v < 12 && acc.valid ?
                                      new { value = acc.value + v, valid = true } :
                                      new { value = acc.value, valid = false },
                            acc => acc.value); 

It's quite ugly, so writting a new AggregateWhile would be nicer:

public static TSource AggregateWhile<TSource>(this IEnumerable<TSource> source, 
                                         Func<TSource, TSource, TSource> func,
                                         Func<TSource, bool> predicate)
{
   using (IEnumerator<TSource> e = source.GetEnumerator()) {
       TSource result = e.Current;
       TSource tmp = default(TSource);
       while (e.MoveNext() && predicate(tmp = func(result, e.Current))) 
            result = tmp;
       return result;
   }
}

(no error checking for brevity)




回答2:


You can write your own extension method. This is not as perfect as the normal Linq methods, I cheated because I already know your requirements to make it simpler. In reality you may want an optional starting value for a and maybe different In and output types for T or other stuff:

public static class Linq
{
  public static T AggregateWhile<T>(this IEnumerable<T> sequence, Func<T, T, T> aggregate, Func<T, bool> predicate)
  {
     T a;
     foreach(var value in sequence)
     {
        T temp = aggregate(a, value);
        if(!predicate(temp)) break;
        a = temp;
     }
     return a;
  }
}



回答3:


Won't this work?

int total = list.Aggregate(0, (a, x) => (a + x) > 12 ? a : a + x);

Using Tuple<bool, int> as accumulator type, to break on first overflow:

int total = list.Aggregate(new Tuple<bool, int>(false, 0),
    (a, x) => a.Item1 || (a.Item2 + x) > 12
    ? new Tuple<bool, int>(true, a.Item2)
    : new Tuple<bool, int>(false, a.Item2 + x)
).Item2;

But it isn't so nice unfortunately.


Start using F#. ;)

let list = [ 1; 2; 3; 4; 5; 1 ]
let predicate = fun a -> a > 12 
let total = list |> List.fold (fun (aval, astate) x ->
    if astate || predicate (aval + x)
    then (aval, true)
    else (aval + x, false)) (0, false)

Tuple unpacking, no new bloat. And when you code it type inference makes it a breeze.




回答4:


I asked this question a while back while encountering a problem that I later reframed into not needing AggregateWhile. But now I've encountered a slightly different problem which undoubtedly requires AggregateWhile or some direct substitute for it.

The solutions proposed by @sloth and @rkrahl are helpful. But they fall short in that the aggregation logic (addition in this case) is repeated twice. This doesn't seem like a big deal for the question's trivial example. But for my real problem, the calculation is complex so writing it twice is unacceptable.

Here's the solution I prefer (short of actual AggregateWhile methods):

class Program
{
    static void Main( string[] args ) { new Program(); }

    public Program()
    {
        var list = new int[] { 1, 2, 3, 4, 5 };
        int total = list
            .Aggregate( new Accumulator( 0 ), ( a, i ) => a.Next( i ), a => a.Total );
    }
}

class Accumulator
{
    public Accumulator( int total )
    {
        this.total = total;
    }

    public Accumulator Next( int i )
    {
        if ( isDone )
            return this;
        else {
            int total = this.total + i;
            if ( total < 12 )
                return new Accumulator( total );
            else {
                isDone = true;
                return this;
            }
        }
    }
    bool isDone;

    public int Total
    {
        get { return total; }
    }
    readonly int total;
}

The ideal solution are fully implemented and tested AggregateWhile methods which correspond to the three Aggregate overloads. Short of that, the above pattern has the advantage that it can leverage the (somewhat lacking) functionality that's already present in the .NET framework.




回答5:


Here is an AggregateWhile with a seed:

public static TAccumulate AggregateWhile<TSource, TAccumulate>(
    this IEnumerable<TSource> source,
    TAccumulate seed,
    Func<TAccumulate, TSource, TAccumulate> func,
    Func<TAccumulate, bool> predicate)
{
    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (func == null)
        throw new ArgumentNullException(nameof(func));

    if (predicate == null)
        throw new ArgumentNullException(nameof(predicate));

    var accumulate = seed;
    foreach (var item in source)
    {
        var tmp = func(accumulate, item);
        if (!predicate(tmp)) break;
        accumulate = tmp;
    }
    return accumulate;
}


来源:https://stackoverflow.com/questions/28427252/aggregate-values-until-a-limit-is-reached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!