History variables: Difference between revisions

m
m (→‎{{header|Java}}: fix several markup problems)
imported>Arakov
 
(4 intermediate revisions by 4 users not shown)
Line 261:
Sum of the historic values: 6
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">define :history [][
init: [
this\record: @[0]
]
]
assign: function [historyVar,newValue][
historyVar\record: historyVar\record ++ newValue
]
 
alias.infix {'-->} 'assign
 
records: function [historyVar][
historyVar\record
]
 
retrieve: function [historyVar][
result: last historyVar\record
historyVar\record: chop historyVar\record
return result
]
 
current: function [historyVar][
return last historyVar\record
]
 
do [
h: to :history []
 
print "Assigning three values: 1, 2, 3..."
h --> 1
h --> 2
h --> 3
 
print "\nHistory (oldest values first):"
print [">" records h]
 
print ["\nCurrent value is:" current h]
 
print "\nRecalling the three values..."
loop 1..3 'x ->
print ["- Recalled:" retrieve h]
 
print "\nHistory:"
print [">" records h]
]</syntaxhighlight>
 
{{out}}
 
<pre>Assigning three values: 1, 2, 3...
 
History (oldest values first):
> [0 1 2 3]
 
Current value is: 3
 
Recalling the three values...
- Recalled: 3
- Recalled: 2
- Recalled: 1
 
History:
> [0] </pre>
 
=={{header|AspectJ}}==
Line 458 ⟶ 522:
{{out}}
<pre>foobar <- foo <- 5
</pre>
 
=={{header|C++}}==
C++ does not have history variables, but they can easily by implemented with a generic class.
<syntaxhighlight lang="c++">
#include <deque>
#include <iostream>
#include <string>
 
template <typename T>
class with_history {
public:
with_history(const T& element) {
history.push_front(element);
}
 
T get() {
return history.front();
}
 
void set(const T& element) {
history.push_front(element);
}
 
std::deque<T> get_history() {
return std::deque<T>(history);
}
 
T rollback() {
if ( history.size() > 1 ) {
history.pop_front();
}
return history.front();
}
 
private:
std::deque<T> history;
};
 
int main() {
with_history<double> number(1.2345);
std::cout << "Current value of number: " << number.get() << std::endl;
 
number.set(3.4567);
number.set(5.6789);
 
std::cout << "Historical values of number: ";
for ( const double& value : number.get_history() ) {
std::cout << value << " ";
}
std::cout << std::endl << std::endl;
 
with_history<std::string> word("Goodbye");
word.set("Farewell");
word.set("Hello");
 
std::cout << word.get() << std::endl;
word.rollback();
std::cout << word.get() << std::endl;
word.rollback();
std::cout << word.get() << std::endl;
word.rollback();
std::cout << word.get() << std::endl;
word.rollback();
}
</syntaxhighlight>
{{ out }}
<pre>
Current value of number: 1.2345
Historical values of number: 5.6789 3.4567 1.2345
 
Hello
Farewell
Goodbye
Goodbye
</pre>
 
Line 753 ⟶ 892:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'collections;
Line 805 ⟶ 944:
console.printLine(o);
o.forEach:(printingLn);
o.undo().undo().undo();
Line 1,690 ⟶ 1,829:
hist: seq[T]
 
ffuncfunc initHistoryVar[T](value: T = T.default): HistoryVar[T] =
## Initialize a history variable with given value.
result.hist.add value
Line 2,875 ⟶ 3,014:
{{trans|Kotlin}}
Not built in but we can soon make a suitable class.
<syntaxhighlight lang="ecmascriptwren">class HistoryVariable {
construct new(initValue) {
_history = [initValue]
Anonymous user