Smart pascal source code
type
TMyRange = class
FMin, FMax: Integer;
constructor Create(min, max: Integer);
function Contains(i: Integer): Boolean;
function ContainsArray(i: array of Integer): Boolean;
class operator IN Integer uses Contains;
class operator IN array of Integer uses ContainsArray;
end;
constructor TMyRange.Create(min, max: Integer);
begin
FMin := min;
FMax := max;
end;
function TMyRange.Contains(i: Integer): Boolean;
begin
Result := (i >= FMin) and (i <= FMax);
end;
function TMyRange.ContainsArray(i: array of Integer): Boolean;
var k: Integer;
begin
for k in i do
if k not in Self then
Exit(False);
Result := True;
end;
var range1to5 := TMyRange.Create(1, 5);
var range3to7 := TMyRange.Create(3, 7);
var i: Integer;
WriteLn('Range 1-5:');
for i := 1 to 9 do
if i in range1to5 then
WriteLn(i);
WriteLn('');
WriteLn('Range 3-7:');
for i := 1 to 9 do
if i in range3to7 then
WriteLn(i);
WriteLn('');
var vals: array of Integer;
vals := [1, 3];
WriteLn(vals in range1to5);
WriteLn(vals in range3to7);
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Range 1-5:
12345
Range 3-7:
34567
True
False
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
var TMyRange = {
$ClassName:"TMyRange",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.FMax = $.FMin = 0;
}
,Contains:function(Self, i$1) {
return i$1>=$Check(Self,"").FMin&&i$1<=$Check(Self,"").FMax;
}
,ContainsArray:function(Self, i$2) {
var Result = false;
var k = 0;
var a$1 = 0;
var $temp1;
for(a$1=0,$temp1=i$2.length;a$1<$temp1;a$1++) {
k = $DIdxR(i$2,a$1,"");
if (!TMyRange.Contains(Self,k)) {
return false;
}
}
Result = true;
return Result
}
,Create$3:function(Self, min, max) {
$Check(Self,"").FMin = min;
$Check(Self,"").FMax = max;
return Self
}
,Destroy:TObject.Destroy
};
var range1to5 = null,
range3to7 = null,
i = 0;
var vals = [];
/* <<< main JS >>> */
range1to5 = TMyRange.Create$3($New(TMyRange),1,5);
range3to7 = TMyRange.Create$3($New(TMyRange),3,7);
WriteLn("Range 1-5:");
for(i=1;i<=9;i++) {
if (TMyRange.Contains(range1to5,i)) {
WriteLn(i);
}
}
WriteLn("");
WriteLn("Range 3-7:");
for(i=1;i<=9;i++) {
if (TMyRange.Contains(range3to7,i)) {
WriteLn(i);
}
}
WriteLn("");
vals = [1, 3].slice();
WriteLn(TMyRange.ContainsArray(range1to5,vals));
WriteLn(TMyRange.ContainsArray(range3to7,vals));