History variables: Difference between revisions
Content added Content deleted
m (→{{header|Lua}}) |
(Add entry for SenseTalk) |
||
Line 2,367: | Line 2,367: | ||
scala> h.undo |
scala> h.undo |
||
res36: Int = 3</lang> |
res36: Int = 3</lang> |
||
=={{header|SenseTalk}}== |
|||
SenseTalk does not have native support for history variables, but here is a simple object that can be instantiated to provide a history: |
|||
<lang sensetalk>// HistoryVariable.script |
|||
properties |
|||
history: [], -- a list of all historical values |
|||
asTextFormat:"[[the last item of my history]]" -- always display the last value |
|||
end properties |
|||
to set newValue |
|||
push newValue nested into my history |
|||
end set |
|||
to handle value |
|||
return the last item of my history |
|||
end value |
|||
to rollback |
|||
pop my history |
|||
return it |
|||
end rollback |
|||
</lang> |
|||
Here is an example how this could be used (note that variables in SenseTalk may hold any type of value): |
|||
<lang sensetalk>set x to be a new HistoryVariable |
|||
tell x to set "Hello" |
|||
put "x is now" && x -- display the current value |
|||
tell x to set 88 |
|||
put "x is now" && x |
|||
tell x to set "World" |
|||
put "x is now" && x |
|||
put "History of x:" && x's history -- non-destructively display the history |
|||
tell x to set [9,6,3] -- set it to a list |
|||
put "x is now" && x |
|||
put "History of x:" && the history of x |
|||
put "Rolling back:" && x.rollback -- remove the last value |
|||
put "After rollback, x is now" && x |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
x is now Hello |
|||
x is now 88 |
|||
x is now World |
|||
History of x: ["Hello",88,"World"] |
|||
x is now [9,6,3] |
|||
History of x: ["Hello",88,"World",[9,6,3]] |
|||
Rolling back: [9,6,3] |
|||
After rollback, x is now World |
|||
</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |