Open
Description
The following helper functions check arrays for all <0, ≤0, 0, ≥0, >0 and ≠0 entries:
// Check if all elements of a non-empty array are zero
function ArrayIsZero(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if not IsZero(Elem) then
Exit;
Result := True;
end;
// Check if all elements of a non-empty array are <> 0
function ArrayIsNonZero(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if IsZero(Elem) then
Exit;
Result := True;
end;
// Check if all elements of a non-empty array are > 0
function ArrayIsPositive(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if Sign(Elem) <> PositiveValue then
Exit;
Result := True;
end;
// Check if all elements of a non-empty array are < 0
function ArrayIsNegative(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if Sign(Elem) <> NegativeValue then
Exit;
Result := True;
end;
// Check if all elements of a non-empty array are <= 0
function ArrayIsNonPositive(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if Sign(Elem) = PositiveValue then
Exit;
Result := True;
end;
// Check if all elements of a non-empty array are >= 0
function ArrayIsNonNegative(const A: array of Extended): Boolean;
begin
Assert(Length(A) > 0);
Result := False;
for var Elem in A do
if Sign(Elem) = NegativeValue then
Exit;
Result := True;
end;
This issue was extracted from issue #16
Metadata
Metadata
Assignees
Projects
Status
Considering
Milestone
Relationships
Development
No branches or pull requests
Activity
delphidabbler commentedon Jan 10, 2025
The proposed routines can be generalised / replaced with the more general function which is inspired by the JavaScript
Array.every()
function:The possible overloads are obvious.
delphidabbler commentedon Jan 10, 2025
Further drawing from JavaScript we could have, in addition to the above comment, the
JavaScript.some()
inspired:delphidabbler commentedon Jan 10, 2025
This comment is now addressed by issue #43
Generalising
ArrayEvery
andArraySome
discussed above, we could add generic versions to theTArrayUtils<T>
record:Example:
Here
EveryALT0
gets set toFalse
(Not every element of the array is < 0) andSomeAGTE0
isTrue
(at least one element of the array is >= 0).TArrayUtils
#43