Retro: J2ME TCL


After I was unable to answer the call in her phone, I decided that something had to be done. Specialists argue that all is not lost and with the help of special technology to keep up with the younger generation. One such tool is N-Back. As with cell phone with touchscreen I can't do it (vicious circle turns), I tried to find such an application under J2ME. Not found and decided to write himself. But Scala and Clojure don't support J2ME, and learn Java training have not written the program I would be hard. After some hugoline the solution was found — Hecl, slightly revised Tcl.

I must say that was programmed in Tcl I am very long ago, when I was working on the SGI O2 computer and envied those who can play the "lines" (aka "balls"). To the appearance of applications I am not very demanding, and using Tcl library Tk this problem I decided.
Tcl — a simple imperative scripting language, with a syntax resembling Unix Shell (and sometimes used in its quality). For extensibility it can be compared to Fort and Lisp. Simplicity of embedding it in applications written in C made it popular among developers of CAD.
Hecl has inherited many features of Tcl, only integriruetsa with Java, not C. Razrabot simplifies the presence of a REPL, in contrast to the classical tclsh supports command history and line editing.
Hecl there is a ready MIDlet with examples and even a minimal development environment. In a jar archive is a script file.hcl is enough to replace it (not forgetting to touch up .jad) that would run your script on the J2ME platform.

So let's start. We need to get a random symbol
set alph {A C G T}
set alphsize [llen $alph]

proc rand {} {
global alph alphsize
lindex $alph [* [random] $alphsize]
}

The variable assignment is performed by the command set. The name of the variable is written just like in Python, and using it is necessary to add ' $ ' as in Perl. If you wish, you can write
set aaa bbb
set $aaa ccc
and variable bbb provoide the value "ccc".
Line, as in the Unix Shell, it is usually not necessary to enclose it in quotes. If still have, besides the usual double quotes you can use balanced braces ({}) — string can be "nested".
The string is split at the space (a list) and the first word is interpreted as the name of the procedure — here, Tcl is a bit like Lisp. Nested command calls other commands are enclosed in square brackets ([]).
Team + and *, a new feature in Hecl. In the original Tcl had to call a special DSL
expr 1 + 2 * 3
almost like a Shell (only * do not escapethe).

Little psychology and Cervera
In this implementation of N-Back used symbols from a small alphabet with equal probability. If you increase the alphabet, the match will be a very rare and play become too boring. You can make the generation of characters more complex Markov process (the probability depends on changing States), which will make the match quite often. There is another interesting point — how the brain is able to analyze hidden Markov models and consider Banovo probability for detecting coincidences. Instead of symbols you can use symbols (Chinese characters or chemical formula combine training with learning) or sounds.


the
set nback {aa aa}
set last ""

proc getnext {} {
global nback last
set nbach [lappend [uses the lset $nback 0] $last]
set last [rand]
return $last
}

proc first {} {
global nback last
eq $last [lindex $nback 0]
}

N the name N-Back is set to the initial length of the list. I N==2 is enough :-).
The initial values of the selected story does not coincide with the characters of the alphabet for simplification. As for the prettiness I wasn't chasing, the first N-1 characters, generadas to fill the buffer, go to the counter guessed.
the
set stats {0 0 0 0}
update proc k {
global stats
uses the lset $stats $k [1+ [lindex $stats $k]]
set stats
}

Procedure of counting statistics. 1+ — the name of the function adding unit. In the array stats are stored successfully seen the number of mismatches, mismatches wrongly seen, wrongly detected matches successfully seen a match. This sequence was chosen out of convenience of implementation, but proved to be convenient for perception.

The following code I cargoculte from the original script.hcl
the
PrintLn proc {W txt} {
global MIDL
$g clear
$g string [list 4 $MIDL] $txt nw
}

proc EventHandler {c e} {
global last MIDL
set reason [$e cget -reason]
if {!= 5 $reason} {return}
set MIDL [/ [$c cget -height] 2]
set keycode [$e cget -keycode]
set res [first]
set sym [getnext]

set g [$c graphics]
set k [eq 49 $keycode]
if {eq $k $res} {
PrintLn $g "[update [+ $res $res $k]] Ok $last"
} else {
PrintLn $g "[update [+ $res $res $k]] Fail $last"
}
}

set c [lcdui.canvas -title "N-Back" -fullscreen 1-eventhandler EventHandler]
$c setcurrent

Here you can watch the object-oriented features to Tcl object that masquerades as a procedure stored in a variable, and the message is marked string constants in the arguments. "Designer" lcdui.canvas creates a "canvas object" and hangs on a event handler. My handler detects the events with reason 5 (push press). Key "1" is processed as seen by the coincidence, and the rest — as seen mismatch. Not sure what a good choice, but too lazy to experiment — the procedure of downloading this on the phone the most difficult in the whole design :-).

In General, Hecl can be recommended for rapid development/prototyping and the like embedded in Java applications language, including legacy platforms.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Yandex.Widget + adjustIFrameHeight + MooTools