Skip to content

Commit

Permalink
add specialized join for springless, add map for TCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Partouf committed Mar 21, 2024
1 parent 1a14b7b commit d2edd04
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Underscore.Delphi.Springless.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
interface

uses
System.Classes,
System.Generics.Collections,
System.SysUtils;

Expand All @@ -16,6 +17,7 @@ _ = class
class function Map<T, S>(const List: TList<T>; const MapFunc: _Func<T, S>): TList<S>; overload;
class function Map<T, S>(const List: TEnumerable<T>; const MapFunc: _Func<T, S>): TList<S>; overload;
class function Map<T, V, S>(const List: TDictionary<T, V>; const MapFunc: _Func<TPair<T,V>, S>): TList<S>; overload;
class function Map<T: TCollectionItem; S>(const List: TCollection; const MapFunc: _Func<T, S>): TList<S>; overload;

class function Reduce<T>(const List: TEnumerable<T>; const ReduceFunc: _Func<T, T, T>; const InitialValue: T): T; overload;
class function Reduce<T,S>(const List: TEnumerable<T>; const ReduceFunc: _Func<S, T, S>; const InitialValue: S): S; overload;
Expand All @@ -28,7 +30,8 @@ _ = class

class function Filter<T>(const List: TList<T>; const Predicate: _Predicate<T>): TList<T>; overload;

class function Join<T>(const List: TList<T>; const JoinFunc: _Func<T, string>; const Separator: string): string;
class function Join<T>(const List: TList<T>; const JoinFunc: _Func<T, string>; const Separator: string): string; overload;
class function Join(const List: TList<string>; const Separator: string): string; overload;

class function Find<T>(const List: TList<T>; const Predicate: _Predicate<T>): T;
class function FindOrDefault<T>(const List: TList<T>; const Predicate: _Predicate<T>; const Default: T): T;
Expand Down Expand Up @@ -63,6 +66,16 @@ class function _.Map<T, S>(const List: TEnumerable<T>; const MapFunc: _Func<T, S
Result.Add(MapFunc(Item));
end;

class function _.Map<T, S>(const List: TCollection; const MapFunc: _Func<T, S>): TList<S>;
var
Item: TCollectionItem;
Idx: Integer;
begin
Result := TList<S>.Create;
for Idx := 0 to List.Count - 1 do
Result.Add(MapFunc(List.Items[Idx] as T));
end;

class function _.Map<T, V, S>(const List: TDictionary<T, V>; const MapFunc: _Func<TPair<T, V>, S>): TList<S>;
var
Item: TPair<T, V>;
Expand Down Expand Up @@ -122,6 +135,21 @@ class function _.FindOrDefault<T>(const List: TList<T>; const Predicate: _Predic
end;
end;

class function _.Join(const List: TList<string>; const Separator: string): string;
var
Value: string;
begin
Result := String.Empty;

for Value in List do
begin
if Result.IsEmpty then
Result := Value
else
Result := Result + Separator + Value;
end;
end;

class function _.Join<T>(const List: TList<T>; const JoinFunc: _Func<T, string>; const Separator: string): string;
var
Value: string;
Expand Down

0 comments on commit d2edd04

Please sign in to comment.