6 ms·
I'm confused as to why your switch is so complicated. Aren't all the cases but the default one actually the same? Why is the "return st(key); break;" repeated
by jrmg 15y ago
I'm confused as to why your switch is so complicated. Aren't all the cases but the default one actually the same? Why is the "return st(key); break;" repeated?
Anyway, it's a while since I've done any Java programming, so I might have my regex syntax incorrect, but:
private boolean handle_key(int key) {
if(KeyEvent.keyCodeToString(key).matches("KEYCODE_[0-9QWERTYUIOP]") {
return st(key);
}
return false;
}
is a lot shorter and easier to read than the Clojure code. It's not as efficient as the Java code you have, of course, but I suspect the Clojure code ends up rather inefficient too with all the taking apart and gluing together of symbol names.
- tensor 15y agoThe Clojure code isn't particularly well written, and given the apparent goal here using a regex is hugely overkill. Despite being inelegant, enumerating all the valid keycodes to a set is the most appropriate answer. Clojure provides a constant time dispatch function called case that is perfect for this.
- Jach 15y agoWoops, messed up the Java code since I never actually ran it. ;) Each st(key) should be replaced by st(0), st(1), ..., st(9).