History variables: Difference between revisions

Added Delphi example
(Changed interface to "initHistoryVar".)
(Added Delphi example)
Line 568:
2013-Jan-19 23:04:55.1662596; goodby
</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<lang Delphi>
program History_variables;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
type
THistoryVarType = record
value: variant;
timestamp: TDateTime;
function ToString: string;
end;
 
THistoryVar = record
Fvalue: variant;
FHistory: TArray<THistoryVarType>;
private
procedure SetValue(const Value: Variant);
function GetTimestamp: TDateTime;
public
property History: TArray<THistoryVarType> read FHistory;
constructor Init(val: variant);
function Dump: string;
function Recall(steps: Integer): variant; overload;
function Recall(timestamp: TDateTime): variant; overload;
property Timestamp: TDateTime read GetTimestamp;
property Value: Variant read Fvalue write SetValue;
end;
 
{ THistoryVar }
 
function THistoryVar.Dump: string;
begin
Result := '';
for var h in FHistory do
Result := Result + h.ToString + #10;
end;
 
function THistoryVar.GetTimestamp: TDateTime;
begin
if Length(FHistory) = 0 then
exit(0);
Result := FHistory[high(FHistory)].timestamp;
end;
 
constructor THistoryVar.Init(val: variant);
begin
SetLength(Fhistory, 0);
SetValue(val);
end;
 
function THistoryVar.Recall(timestamp: TDateTime): variant;
begin
for var h in FHistory do
begin
if h.timestamp = timestamp then
exit(h.value);
end;
result := Fvalue;
end;
 
function THistoryVar.Recall(steps: Integer): variant;
begin
if (steps <= 1) or (steps >= Length(FHistory)) then
exit(value);
 
result := FHistory[Length(FHistory) - steps - 1].value;
end;
 
procedure THistoryVar.SetValue(const Value: Variant);
begin
SetLength(FHistory, Length(FHistory) + 1);
FHistory[High(FHistory)].Value := Value;
FHistory[High(FHistory)].timestamp := now;
Fvalue := Value;
end;
 
{ THistoryVarType }
 
function THistoryVarType.ToString: string;
var
dt: string;
begin
DateTimeToString(dt, 'mm/dd/yyyy hh:nn:ss.zzz', timestamp);
Result := format('%s - %s', [dt, Value]);
end;
 
begin
var v := THistoryVar.Init(0);
v.Value := True;
 
Sleep(200);
if v.Value then
v.Value := 'OK';
 
Sleep(100);
if v.Value = 'OK' then
v.Value := PI;
 
writeln('History of variable values:'#10);
Writeln(v.Dump);
 
Writeln('Recall 2 steps');
Writeln(v.Recall(2));
 
var t := v.History[3].timestamp;
Writeln(#10'Recall to timestamp ', datetimetostr(t));
Writeln(v.Recall(t));
 
readln;
end.</lang>
{{out}}
<pre>History of variable values:
 
02/28/2021 19:50:32.512 - 0
02/28/2021 19:50:32.512 - True
02/28/2021 19:50:32.712 - OK
02/28/2021 19:50:32.813 - 3,14159265358979
 
Recall 2 steps
True
 
Recall to timestamp 28/02/2021 19:50:32
3,14159265358979</pre>
 
=={{header|EchoLisp}}==
478

edits