我可以运行带有预定义参数的排队Lambda吗?

huangapple go评论50阅读模式
英文:

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&lt;Movement&gt; movementDeque = new ArrayDeque&lt;&gt;();

    void move(MovementType type);

}

...

public void addToQueue(Direction direction){

    MovementType type = switch (direction) {
        case LEFT -&gt; MovementType.TURN_LEFT;
        case RIGHT -&gt; MovementType.TURN_RIGHT;
        case UP -&gt; MovementType.FORWARD;
        case DOWN -&gt; MovementType.BACKWARD;
    }
    
    Movement.movementDeque.offer((m) -&gt; 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 -&gt; MovementType.TURN_LEFT;
        case RIGHT -&gt; MovementType.TURN_RIGHT;
        case UP -&gt; MovementType.FORWARD;
        case DOWN -&gt; MovementType.BACKWARD;
    }
    
    Movement.movementDeque.offer(() -&gt; subject.move(type));

}

Runnable movement = movementDeque.poll();
movement.run();

Which should achieve the same result you are expecting

huangapple
  • 本文由 发表于 2023年2月19日 21:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定