How can I convert this piece of code to functional approach? In akka I've seen Aktor::become() method that allows to overwrite the behaviour. How can I achieve something similar?
class Updater {
var move: String = ""
val step = 50
def handler(key: KeyEvent) = {
if (key.getCode.equals(KeyCode.Up.delegate)) move = "up"
if (key.getCode.equals(KeyCode.Down.delegate)) move = "down"
if (key.getCode.equals(KeyCode.Left.delegate)) move = "left"
if (key.getCode.equals(KeyCode.Right.delegate)) move = "right"
}
def update(state: State): State = {
val result = move match {
case "up" => State(state.x, state.y - step)
case "down" => State(state.x, state.y + step)
case "left" => State(state.x - step, state.y)
case "right" => State(state.x + step, state.y)
case _ => state
}
move = ""
result
}
}
movein a field, you might be able to pass it as a parameter so you wouldn't have to reset it each time. Perhaps you can directly call the update method from the handler method if you changed the signature of the latter to include a State object? Also, you can use==instead of.equals, I'm pretty sure.