I am using Spring4D for all collections.
Now there is a situation where I have to know whether the current value of the enumerator is the first (which is easy) or the last (which is hard) in the collection.
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Spring.Collections;
var
Enumerable: IEnumerable<Integer>;
Enumerator: IEnumerator<Integer>;
begin
Enumerable := TEnumerable.Query<Integer>(TArray<Integer>.Create(1, 2, 3, 4, 5)
) as IEnumerable<Integer>;
Enumerator := Enumerable.GetEnumerator;
while Enumerator.MoveNext do
begin
WriteLn('Value = ', Enumerator.Current);
WriteLn('First in collection? ', Enumerator.CurrentIsFirst);
WriteLn('Last in collection? ', Enumerator.CurrentIsLast);
end;
ReadLn;
end.
CurrentIsFirst
could be implemented using a local Boolean which is reset once the first value has passed.
However I don't know an easy way to implement CurrentIsLast
.
It should be able to process lazy collections as they may contain too many values to fit into memory.
How can I implement such a CurrentIsLast
function?