History variables: Difference between revisions
J
m (Formatting. & remove TLA.) |
(J) |
||
Line 18:
For extra points, if the language of choice does not support history variables, demonstrate how this might be implemented.
=={{header|J}}==
J does not natively support "history variables", but the functionality is easy to add:
<lang j>varref_hist_=:'VAR','_hist_',~]
set_hist_=:4 :0
V=.varref x
if.0>nc<V do.(<V)=:''end.
(<V)=.V~,<y
y
)
getall_hist_=:3 :0
(varref y)~
)
length_hist_=: #@getall
get_hist_=: _1 {:: getall</lang>
Example use:
<lang j> 'x' set_hist_ 9
9
'x' set_hist_ 10
10
'x' set_hist_ 11
11
get_hist_ 'x'
11
length_hist_ 'x'
3
getall_hist_ 'x'
┌─┬──┬──┐
│9│10│11│
└─┴──┴──┘</lang>
Note that each value is contained in a box, so different values do not need to be type compatible with each other. If this is considered a defect then assertions could be added to enforce type compatibility across assignments.
Note that only nouns are supported here: If you want to store verbs using this mechanism you will need to use their gerunds.
=={{header|Protium}}==
|