I am working on part of a program (concerning speech recognition and a remote control car), where the code transmit(XXXXX); disableAutoMode();
is repeated many times. For curiosity's sake I would like to convert this into a lambda function similar to this var f = p -> transmit(p); disableAutoMode();
(forgive the var
; I don't know what the type of the expression would be) and then call it in a way similar to this: f("s");
, f("a");
and f("f");
or something similar to f.call("s");
, f.call("a");
and f.call("f");
.
What is the correct syntax to use a simple lambda function in Java similar to how I described above? (What should I put down as it's type instead of saying var
?)
Here is the block of code, if you're curious:
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i("onresult",text);
ToastMaster(text);
switch (text) {
case "forward":
case "go forward":
transmit("f");
disableAutoMode();
break;
case "go back":
case "go backward":
case "back":
case "backward":
case "reverse":
transmit("b");
disableAutoMode();
break;
case "skid left":
case "go left":
transmit("l");
disableAutoMode();
break;
case "skid right":
case "go right":
transmit("r");
disableAutoMode();
break;
case "turn left":
transmit("q");
disableAutoMode();
break;
case "turn right":
transmit("e");
disableAutoMode();
break;
case "reverse left":
transmit("z");
disableAutoMode();
break;
case "reverse right":
transmit("x");
disableAutoMode();
break;
case "stop":
disableAutoMode();
break;
case "automatic":
toggleAutoMode(null);
break;
case "enable automatic mode":
enableAutoMode();
break;
case "disable automatic mode":
disableAutoMode();
break;
}
}
}