static interface Algorithm { S predict(int max); void feed(S s); } static class LuaAlgo implements Algorithm { LuaValue predict, feed; S feeding; *(S code) { Sandbox s = luaSandbox(); evalLua(s, code); //print("KEYS: " + structure(s.keys())); predict = s.get("predict"); if (predict.isnil()) predict = s.get("p"); assertFalse(predict.isnil()); } public S predict(int max) { LuaValue s = predict.call(Lua.value(max), feeding != null ? Lua.value(feeding) : Lua.NIL); feeding = null; ret s.isnil() ? "" : s.tojstring(); } public void feed(S s) { feeding = s; } } static Algorithm makeLuaTextPredictAlgo(S desc) { ret new LuaAlgo(dropPrefix("L ", desc)); }