History variables: Difference between revisions

Line 1,748:
Current value: 42
</pre>
 
=={{header|Phix}}==
No native support, but trivial to implement.<br>
If you only need the history for a single variable, you can just do this:
<lang Phix>sequence history = {}
 
type hvt(object o)
history = append(history,o)
return true
end type
 
hvt test = 1
test = 2
test = 3
?{"current",test}
?{"history",history}</lang>
{{out}}
<pre>
{"current",3}
{"history",{1,2,3}}
</pre>
Multiple history variables would require that routines must be invoked to create, update, and inspect them.<br>
Writing this as a separate reusable component (but omitting destroy/freelist handling for simplicity):
<lang Phix>-- history.e
sequence histories = {}
 
global function new_history_id(object v)
histories = append(histories,{v})
return length(histories)
end function
 
global procedure set_hv(integer hv, object v)
histories[hv] = append(histories[hv],v)
end procedure
 
global function get_hv(integer hv)
return histories[hv][$]
end function
 
global function get_hv_full_history(integer hv)
return histories[hv]
end function</lang>
And use it like this
<lang Phix>include history.e
 
constant test2 = new_history_id(1)
set_hv(test2, 2)
set_hv(test2, 3)
?{"current",get_hv(test2)}
?{"history",get_hv_full_history(test2)}</lang>
Same output. Of course test2 does not ''have'' to be a constant, but it may help.
 
=={{header|PicoLisp}}==
7,795

edits