Idiom #57 Filter list
Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.
function Filter(vv:integer):boolean;
begin
result := vv mod 2= 0;
end;
type TFilter=function(v:integer):boolean;
function FilteredArray(const x:TBoundArray;p:TFilter):TBoundArray;
var
Idx: Integer;
v : Integer;
begin
setlength(result,high(x)+1);
Idx := 0;
for v in x do
if p(v) then
begin
result[Idx] := v;
inc(Idx);
end;
setlength(result,Idx);
end;
[...]
y := FilteredArray(x,@Filter);
The function Filter defines a demo-filter.
This is/can be passed to the FilteredArray-function.
TBoundArray is a standard type for array of integer;
On some implementation's (e.G: delphi) you have to omit the @ before Filter.
This is/can be passed to the FilteredArray-function.
TBoundArray is a standard type for array of integer;
On some implementation's (e.G: delphi) you have to omit the @ before Filter.