This repository has been archived on 2024-12-25. You can view files and clone it, but cannot push or open issues or pull requests.
OldPascalProjects/Samples/LanguageFeatures/ForeachIEnumerable.pas
2023-06-20 21:52:24 +03:00

46 lines
1.2 KiB
ObjectPascal
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Пример иллюстрирует реализацию классом интерфейса IEnumerable
// для использования его в операторе foreach
type
// Генератор чисел Фибоначчи
FibGen = class(IEnumerable<integer>, IEnumerator<integer>)
private
a,b,n,i: integer;
public
constructor Create(n: integer);
begin
i := -1;
a := 0;
b := 1;
Self.n := n;
end;
function Get_Current: integer;
begin
if i=0 then
Result := 1
else Result := b;
end;
function System.Collections.IEnumerator.Get_Current: object := Get_Current;
function GetEnumerator: IEnumerator<integer> := Self;
function System.Collections.IEnumerable.GetEnumerator: System.Collections.IEnumerator := Self;
function MoveNext: boolean;
begin
i += 1;
Result := i<n;
if i=0 then exit;
(a,b) := (b,a+b);
end;
property Current: integer read Get_Current;
procedure Reset;
begin
end;
procedure Dispose;
begin
end;
end;
begin
writeln('Числа Фибоначчи');
var f := new FibGen(25);
foreach var x in f do
Print(x);
end.