-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper functions to check arrays that conform to various constraints #40
Comments
The proposed routines can be generalised / replaced with the more general function which is inspired by the JavaScript function ArrayEvery(const A: array of Extended; const Constraint: TPredicate<Extended>): Boolean;
begin
Assert(Length(A) > 0); // could possibly return False in case of empty array
Result := False;
for var Elem in A do
if not Constraint(Elem) then
Exit;
Result := True;
end; The possible overloads are obvious. |
Further drawing from JavaScript we could have, in addition to the above comment, the function ArraySome(const A: array of Extended; const Constraint: TPredicate<Extended>): Boolean;
begin
Assert(Length(A) > 0); // could possibly return False in case of empty array
Result := True;
for var Elem in A do
if Constraint(Elem) then
Exit;
Result := False;
end; |
This comment is now addressed by issue #43Generalising type
TArrayUtils<T> = record
public
...
class function Every<T>(const A: array of T;
const Constraint: TPredicate<T>): Boolean; static;
class function Some<T>(const A: array of T;
const Constraint: TPredicate<T>): Boolean; static;
...
end;
...
class function TArrayUtils.Every<T>(const A: array of T;
const Constraint: TPredicate<T>: Boolean;
begin
Assert(Length(A) > 0); // could possibly return False in case of empty array
Result := False;
for var Elem in A do
if not Constraint(Elem) then
Exit;
Result := True;
end;
class function TArrayUtils.Some<T>(const A: array of T;
const Constraint: TPredicate<T>): Boolean;
begin
Assert(Length(A) > 0); // could possibly return False in case of empty array
Result := True;
for var Elem in A do
if Constraint(Elem) then
Exit;
Result := False;
end;
... Example: var EveryALT0 := TArrayUtils.Every<Integer>(
[0,-1,2,-3,4],
function (Elem: Integer): Boolean
begin
Result := Elem < 0;
end
);
var SomeAGTE0 := TArrayUtils.Some<Integer>(
[0,-1,2,-3,4],
function (Elem: Integer): Boolean
begin
Result := Elem >= 0;
end
); Here |
The following helper functions check arrays for all <0, ≤0, 0, ≥0, >0 and ≠0 entries:
This issue was extracted from issue #16
The text was updated successfully, but these errors were encountered: