Loopless code of the day ;)
Loopless code of the day ;)
function GetCommonDir(const paths: TArray): string;
var
files: IEnumerable;
matchingChars: IEnumerable;
begin
files := TEnumerable.Query(paths);
matchingChars :=
TEnumerable.Select(TEnumerable.Range(0, files.Min(
function(s: string): Integer
begin
Result := Length(s);
end)).Reversed,
function(len: Integer): string
begin
Result := Copy(files.First, 1, len);
end).Where(
function(const possibleMatch: string): Boolean
begin
Result := files.All(
function(const f: string): Boolean
begin
Result := StartsText(possibleMatch, f);
end);
end);
Result := TPath.GetDirectoryName(matchingChars.First);
end;
Ok, I admit, in C# it looks not so weird and much nicer ;p
var MatchingChars =
from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
let possibleMatch = Files.First().Substring(0, len)
where Files.All(f => f.StartsWith(possibleMatch))
select possibleMatch;
var LongestDir = Path.GetDirectoryName(MatchingChars.First());
function GetCommonDir(const paths: TArray
var
files: IEnumerable
matchingChars: IEnumerable
begin
files := TEnumerable.Query
matchingChars :=
TEnumerable.Select
function(s: string): Integer
begin
Result := Length(s);
end)).Reversed,
function(len: Integer): string
begin
Result := Copy(files.First, 1, len);
end).Where(
function(const possibleMatch: string): Boolean
begin
Result := files.All(
function(const f: string): Boolean
begin
Result := StartsText(possibleMatch, f);
end);
end);
Result := TPath.GetDirectoryName(matchingChars.First);
end;
Ok, I admit, in C# it looks not so weird and much nicer ;p
var MatchingChars =
from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
let possibleMatch = Files.First().Substring(0, len)
where Files.All(f => f.StartsWith(possibleMatch))
select possibleMatch;
var LongestDir = Path.GetDirectoryName(MatchingChars.First());
Comments
Post a Comment