using Aggregate from Spring.Collections.Enumerable.pas

Issue #138 closed
Rico Bautsch created an issue

We are using the famous Spring4D Framework. Recently i would like to use the Aggregate functions, defined in Spring.Collections.Enumerable.pas. This unit is not included in the package, so i cannot use it.

Is this a mistake? And why should i implicitly cast to an record, why are Aggregate functions not available in IEnumerable<T>?

So how can i use these functions. Do i have to tweak the packages myself?

Comments (2)

  1. Stefan Glienke repo owner

    Spring.Collections.Enumerable.pas was an experiment that did not work as well as I hoped (compiler issues) - that is why this unit is not present in the packages. IEnumerable<T> cannot have any methods that take generic parameters that is why Aggregate and other methods that you might know from other languages are not present.

    For 1.3 we want to extend Spring.Collections.TEnumerable to provide most of these missing methods. Until then you can write your own Aggregate method by extending (either by helper or inheriting from) Spring.Collections.TEnumerable

    type
      TEnumerableHelper = class helper for TEnumerable
        class function Aggregate<TSource, TAccumulate>(const source: IEnumerable<TSource>;
          const seed: TAccumulate; const func: TFunc<TAccumulate, TSource, TAccumulate>): TAccumulate; static;
      end;
    
    class function TEnumerableHelper.Aggregate<TSource,TAccumulate>(
      const source: IEnumerable<TSource>; const seed: TAccumulate;
      const func: TFunc<TAccumulate, TSource, TAccumulate>): TAccumulate;
    var
      enumerator: IEnumerator<TSource>;
    begin
      Guard.CheckNotNull(Assigned(source), 'source');
      Guard.CheckNotNull(Assigned(func), 'func');
    
      enumerator := source.GetEnumerator;
      Result := seed;
      while enumerator.MoveNext do
        Result := func(Result, enumerator.Current);
    end;
    

    Please show your interest in improving the language by voting on following items:

  2. Log in to comment