Clone wiki

Spring4D / IEnumerable

Introduction

IEnumerable<T> is a very powerful interface implemented by all the Spring4D collections. It's power comes from enabling you to get customized views of a given collection, returning precisely the members of that collection that you want.

It's members allow you to "grab" virtually anything you choose from the given collection. Through the use of simple methods, predicates, and actions, it allows you access to

IEnumerable<T> is an excellent way to expose collections to users in a read-only fashion. Rather than expose, say, the IList interface (which users can add to and delete from), you can expose IEnumerable<T> which gives them access to the collection and the ability to use it in many different ways, all the while allowing you to provide write access in a controlled and desired manner.

IEnumerable<T> enhances the IEnumerable interface.

IEnumerable is declared as follows:

  IEnumerable = interface(IInvokable)
    ['{6BC97F33-C0A8-4770-8E1C-C2017527B7E7}']
    function GetCount: Integer;
    function GetElementType: PTypeInfo;
    function GetIsEmpty: Boolean;
    function AsObject: TObject;
    function GetEnumerator: IEnumerator;
    property Count: Integer read GetCount;
    property ElementType: PTypeInfo read GetElementType;
    property IsEmpty: Boolean read GetIsEmpty;
  end;

IEnumerable<T> is declared as follows:

  IEnumerable<T> = interface(IEnumerable)
    ['{A6B46D30-5B0F-495F-B7EC-46FBC5A75D24}']
    function GetComparer: IComparer<T>;
    function GetEnumerator: IEnumerator<T>;
    function Aggregate(const func: TFunc<T, T, T>): T;
    function All(const predicate: TPredicate<T>): Boolean;
    function Any: Boolean; overload;
    function Any(const predicate: TPredicate<T>): Boolean; overload;
    function Concat(const second: IEnumerable<T>): IEnumerable<T>; overload;
    function Contains(const value: T): Boolean; overload;
    function Contains(const value: T; const comparer: IEqualityComparer<T>): Boolean; overload;
    function Contains(const value: T; const comparer: TEqualityComparison<T>): Boolean; overload;
    function ElementAt(index: Integer): T;
    function ElementAtOrDefault(index: Integer): T; overload;
    function ElementAtOrDefault(index: Integer; const defaultValue: T): T; overload;
    function EqualsTo(const values: array of T): Boolean; overload;
    function EqualsTo(const collection: IEnumerable<T>): Boolean; overload;
    function EqualsTo(const collection: IEnumerable<T>; const comparer: IEqualityComparer<T>): Boolean; overload;
    function First: T; overload;
    function First(const predicate: TPredicate<T>): T; overload;
    function FirstOrDefault: T; overload;
    function FirstOrDefault(const defaultValue: T): T; overload;
    function FirstOrDefault(const predicate: TPredicate<T>): T; overload;
    function FirstOrDefault(const predicate: TPredicate<T>; const defaultValue: T): T; overload;
    procedure ForEach(const action: TAction<T>);
    function Last: T; overload;
    function Last(const predicate: TPredicate<T>): T; overload;
    function LastOrDefault: T; overload;
    function LastOrDefault(const defaultValue: T): T; overload;
    function LastOrDefault(const predicate: TPredicate<T>): T; overload;
    function LastOrDefault(const predicate: TPredicate<T>; const defaultValue: T): T; overload;
{$IFDEF DELPHIXE_UP}
    function Max(const selector: TFunc<T, Integer>): Integer; overload;
{$ENDIF}
    function Max(const comparer: IComparer<T>): T; overload;
    function Max(const comparer: TComparison<T>): T; overload;
    function Min: T; overload;
{$IFDEF DELPHIXE_UP}
    function Min(const selector: TFunc<T, Integer>): Integer; overload;
{$ENDIF}
    function Min(const comparer: IComparer<T>): T; overload;
    function Min(const comparer: TComparison<T>): T; overload;
    function Ordered: IEnumerable<T>; overload;
    function Ordered(const comparer: IComparer<T>): IEnumerable<T>; overload;
    function Ordered(const comparer: TComparison<T>): IEnumerable<T>; overload;
    function Reversed: IEnumerable<T>;
    function Single: T; overload;
    function Single(const predicate: TPredicate<T>): T; overload;
    function SingleOrDefault: T; overload;
    function SingleOrDefault(const defaultValue: T): T; overload;
    function SingleOrDefault(const predicate: TPredicate<T>): T; overload;
    function SingleOrDefault(const predicate: TPredicate<T>; const defaultValue: T): T; overload;
    function Skip(count: Integer): IEnumerable<T>;
    function SkipWhile(const predicate: TPredicate<T>): IEnumerable<T>; overload;
    function SkipWhile(const predicate: TFunc<T, Integer, Boolean>): IEnumerable<T>; overload;
    function Sum: T; overload;
    function Take(count: Integer): IEnumerable<T>;
    function TakeWhile(const predicate: TPredicate<T>): IEnumerable<T>; overload;
    function TakeWhile(const predicate: TFunc<T, Integer, Boolean>): IEnumerable<T>; overload;
    function ToArray: TArray<T>;
    function TryGetFirst(out value: T): Boolean; overload;
    function TryGetFirst(out value: T; const predicate: TPredicate<T>): Boolean; overload;
    function TryGetLast(out value: T): Boolean; overload;
    function TryGetLast(out value: T; const predicate: TPredicate<T>): Boolean; overload;
    function TryGetSingle(out value: T): Boolean; overload;
    function TryGetSingle(out value: T; const predicate: TPredicate<T>): Boolean; overload;
    function Where(const predicate: TPredicate<T>): IEnumerable<T>; overload;
    property Comparer: IComparer<T> read GetComparer;
  end;

Updated