From 0a20c8d32c8b38d500fd0f7a60580680e5899af0 Mon Sep 17 00:00:00 2001 From: Sun Serega Date: Thu, 26 Sep 2024 09:02:31 +0200 Subject: [PATCH] Add `.DistinctBy` (#3122) --- bin/Lib/PABCSystem.pas | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bin/Lib/PABCSystem.pas b/bin/Lib/PABCSystem.pas index 308a1be83..a7f234979 100644 --- a/bin/Lib/PABCSystem.pas +++ b/bin/Lib/PABCSystem.pas @@ -11606,6 +11606,17 @@ begin Result += 1; end; +/// Возвращает элементы последовательности, ключи для которых отличаются, используя компоратор comp +function DistinctBy(Self: sequence of T; by: T->TKey; comp: IEqualityComparer): sequence of T; extensionmethod; +begin + var hs := new HashSet(comp); + foreach var x in Self do + if hs.Add(by(x)) then + yield x; +end; +/// Возвращает элементы последовательности, ключи для которых отличаются, используя компоратор по-умолчанию +function DistinctBy(Self: sequence of T; by: T->TKey); extensionmethod := Self.DistinctBy(by, nil); + // ----------------------------------------------------- //>> Методы расширения списков # Extension methods for List T // -----------------------------------------------------