delphi-collを使ってみる(4) C#のクエリメソッドと比較する

delphi-collを使ってみる(1)
delphi-collを使ってみる(2)
delphi-collを使ってみる(3)
の続編です。

C#のクエリメソッドを使用したコードと同じ処理を書いてみました。
delphi-collを使うと、C#と同じように記述することができました。

■配列から偶数を小さい順に抽出する

C#の例

int[] nums = { 5, 10, 8, 3, 6, 12 };
var posNums = nums.Where(n => n % 2 == 0).Select(r => r).OrderBy(n => n);
foreach(int i in posNums) Console.WriteLine(i);

Delphiの例

var
  nums: TList<Integer>;
  posNums: IEnexCollection<Integer>;
  i: Integer;
begin
  nums := TList<Integer>.Create([1, -2, 3, 0, -4, 5]);
  posNums := nums.Where(function(N: Integer): Boolean begin Exit(N mod 2 = 0) end);
  for i in posNums do
    WriteLn(i);
end;

■1980年以降生まれの人物名を抽出する

C#の例

class Person
{
    public String Name;
    public DateTime BirthDate;
    public Person(String AName, DateTime ABirthDate)
    {
        Name = AName;
        BirthDate = ABirthDate;
    }
}

Person[] persons = { new Person("John",  new DateTime(1984, 1, 4)),
                     new Person("Mary",  new DateTime(1955, 4, 11)),
                     new Person("Ken",   new DateTime(2001, 8, 23)),
                     new Person("Lidia", new DateTime(1978, 3, 10)),
                   };
var youngman = persons.Where(
        person => person.BirthDate.CompareTo(new DateTime(1980, 1, 1)) > 0
        ).Select(person => person.Name);
foreach (String s in youngman) Console.WriteLine(s);

Delphiの例

type
  TPerson = record
    Name: string;
    BirthDate: TDate;
    constructor Create(const AName: string; const ABirthDate: TDate);
  end;

constructor TPerson.Create(const AName: string; const ABirthDate: TDate);
begin
  Name := AName;
  BirthDate := ABirthDate;
end;

var
  persons: TList<TPerson>;
  youngman: IEnexCollection<TValue>;
  s: TValue;
begin
  persons := TList<TPerson>.Create([
    TPerson.Create('John', EncodeDate(1984, 1,  4)),
    TPerson.Create('Mary', EncodeDate(1955, 4, 11)),
    TPerson.Create('Ken',  EncodeDate(2001, 8, 23)),
    TPerson.Create('Lidia',EncodeDate(1978, 3, 10))
  ]);
  youngman := persons.Where(
    function(P: TPerson): Boolean begin
      Exit(CompareDate(P.BirthDate, EncodeDate(1980, 1, 1)) <> LessThanValue)
    end).Op.Select('Name');
  for s in youngman do
    WriteLn(s.AsString);
end;

■最年少の生年月日を抽出する

C#の例

Person[] persons = { new Person("John",  new DateTime(1984, 1, 4)),
                     new Person("Mary",  new DateTime(1955, 4, 11)),
                     new Person("Ken",   new DateTime(2001, 8, 23)),
                     new Person("Lidia", new DateTime(1978, 3, 10)),
                   };
DateTime date = persons.Select(person => person.BirthDate).Max();
Console.WriteLine(date.ToString());

Delphiの例

persons := TList<TPerson>.Create([
  TPerson.Create('John', EncodeDate(1984, 1,  4)),
  TPerson.Create('Mary', EncodeDate(1955, 4, 11)),
  TPerson.Create('Ken',  EncodeDate(2001, 8, 23)),
  TPerson.Create('Lidia',EncodeDate(1978, 3, 10))
]);
date := persons.Op.Select<TDate>('BirthDate').Max();
Writeln(DateToStr(date));

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください