英文:
Can I run queued lambdas with pre-defined arguments?
问题
I'm looking to add lambdas to a queue to be run when the queue is polled, using the arguments passed to them when they were added to the queue. Something like below:
@FunctionalInterface
public interface Movement {
Deque<Movement> movementDeque = new ArrayDeque<>();
void move(MovementType type);
}
...
public void addToQueue(Direction direction){
MovementType type = switch (direction) {
case LEFT -> MovementType.TURN_LEFT;
case RIGHT -> MovementType.TURN_RIGHT;
case UP -> MovementType.FORWARD;
case DOWN -> MovementType.BACKWARD;
}
Movement.movementDeque.offer((m) -> subject.move(type));
}
...
Movement.movementDeque.poll(); // Running the lambda later
Is this possible? None of what I've attempted so far seems to allow me to invoke lambdas using the arguments passed to them when they were defined. Feel like I'm misunderstanding something pretty fundamental here.
英文:
I'm looking to add lambdas to a queue to be run when the queue is polled, using the arguments passed to them when they were added to the queue. Something like below:
@FunctionalInterface
public interface Movement {
Deque<Movement> movementDeque = new ArrayDeque<>();
void move(MovementType type);
}
...
public void addToQueue(Direction direction){
MovementType type = switch (direction) {
case LEFT -> MovementType.TURN_LEFT;
case RIGHT -> MovementType.TURN_RIGHT;
case UP -> MovementType.FORWARD;
case DOWN -> MovementType.BACKWARD;
}
Movement.movementDeque.offer((m) -> subject.move(type));
}
...
Movement.movementDeque.poll(); // Running the lambda later
Is this possible? None of what I've attempted so far seems to allow me to invoke lambdas using the arguments passed to them when they were defined. Feel like I'm misunderstanding something pretty fundamental here.
答案1
得分: 1
不认为可以存储具有特定参数的 lambda 表达式(请记住,lambda 实际上只是一种简化的方法),但你可以将 move 方法更改为不接受任何参数,然后可以简单地将 move 方法排队并像这样调用它:
public void addToQueue(Direction direction){
MovementType type = switch (direction) {
case LEFT -> MovementType.TURN_LEFT;
case RIGHT -> MovementType.TURN_RIGHT;
case UP -> MovementType.FORWARD;
case DOWN -> MovementType.BACKWARD;
}
Movement.movementDeque.offer(() -> subject.move(type));
}
Runnable movement = movementDeque.poll();
movement.run();
这应该能实现你期望的相同结果。
英文:
I dont think its possible to store a lambda with specific paramaters (since remember, a lambda is essentially just a shorthand method), what you could do though, is change the move method to not take any arguments
public interface Movement {
void move();
}
And then you can simply queue the move method and invoke it like so
public void addToQueue(Direction direction){
MovementType type = switch (direction) {
case LEFT -> MovementType.TURN_LEFT;
case RIGHT -> MovementType.TURN_RIGHT;
case UP -> MovementType.FORWARD;
case DOWN -> MovementType.BACKWARD;
}
Movement.movementDeque.offer(() -> subject.move(type));
}
Runnable movement = movementDeque.poll();
movement.run();
Which should achieve the same result you are expecting
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论