History variables: Difference between revisions

Line 1,527:
CheckStringHistoryVariables
</lang>
 
=={{header|Nim}}==
Nim doesn’t provide history variables, but it is easy to implement them.
As Nim is statically typed, we define history variable type in a generic way.
 
<lang Nim>type HistoryVar[T] = object
hist: seq[T]
 
func initHistoryVar[T](): HistoryVar[T] =
## Initialize a history variable with default value (binary zero).
result.hist.add T.default
 
func initHistoryVar[T](value: T): HistoryVar[T] =
## Initialize a history variable with given value.
result.hist.add value
 
func set(h: var HistoryVar; value: h.T) =
## Set the history variable to given value.
h.hist.add value
 
func get(h: HistoryVar): h.T =
## Return the current value of history variable.
h.hist[^1]
 
proc showHistory(h: HistoryVar) =
## Show the history starting from oldest values.
for i, value in h.hist:
echo i, ": ", value
 
func pop(h: var HistoryVar): h.T =
## Pop the current value and return it.
if h.hist.len > 1: h.hist.pop() else: h.hist[0]
 
 
when isMainModule:
 
var h = initHistoryVar[int]() # Initialized to 0.
 
echo "Assigning three values: 1, 2, 3"
h.set(1) # 0, 1
h.set(2) # 0, 1, 2
h.set(3) # 0, 1, 2, 3
 
echo "History (oldest values first):"
h.showHistory()
 
echo "Current value is ", h.get()
 
echo "Recall the three values:"
echo h.pop() # -> 3, last value removed.
echo h.pop() # -> 2, last value removed.
echo h.pop() # -> 1, last value removed.
 
echo "History (note that initial value can never be removed):"
h.showHistory()</lang>
 
{{out}}
<pre>Assigning three values: 1, 2, 3
History (oldest values first):
0: 0
1: 1
2: 2
3: 3
Current value is 3
Recall the three values:
3
2
1
History (note that initial value can never be removed):
0: 0</pre>
 
=={{header|Oberon-2}}==
Anonymous user