Hi all.
Hi all.
How can I cast TArray to IEnumerable?
I need this to avoid duplicate code:
class function TCheck.Every(Source: IEnumerable; Lambda: TPredicateRef): Boolean;
var
X: T;
begin
Result := True;
for X in Source do
if not Lambda(X) then
Exit(False);
end;
class function TCheck.Every(Source: TArray; Lambda: TPredicateRef): Boolean;
var
X: T;
begin
Result := True;
for X in Source do
if not Lambda(X) then
Exit(False);
end;
How can I cast TArray
I need this to avoid duplicate code:
class function TCheck
var
X: T;
begin
Result := True;
for X in Source do
if not Lambda(X) then
Exit(False);
end;
class function TCheck
var
X: T;
begin
Result := True;
for X in Source do
if not Lambda(X) then
Exit(False);
end;
You cannot cast TArray to IEnumerable. What you can do is create a helper method of some description that will create a newly minted IEnumerable when passed a TArray. I would expect that frameworks like Spring have such things ready made.
ReplyDeleteYup, TCollections.Query (1.1) or TEnumerable.Query (develop/1.2) wraps the passed TArray into an IEnumerable.
ReplyDeleteAlso Spring4D already contains the IEnumerable.All method that does exactly what your Every method does.
Thanks for the advice. I chose this way to solve my problem: https://github.com/magicxor/MapReduce/blob/master/Source/MapReduce.pas
ReplyDeleteNonamed Pravdin This is generics via templates a-la C++ ! :-)
ReplyDelete+David Manual inclusion is a giant step from templates
ReplyDeleteDavid Heffernan yes, it allows much more granularity. I like to think about $include as the same as $region.
ReplyDeleteDavid Berneda I don't think so. Template specialization in C++ allows huge granularity and flexibility. Template meta programming, being Turing complete. Try that with $INCLUDE.
ReplyDeleteI could find myself liking loading code off into include files if the editor would blend such code in nicely, but jumping through several files that just contain tiny snippets of code without context is just f***ing horrible and only a cure to a symptom.
ReplyDelete