Closures and event handling: Correct Solution


Suppose we are in an environment where CLICK, TICK, and KEY events can occur, and we have the following functions available:

onClick(closure)

Executes closure after every subsequent CLICK event.

onKeyPress(closure)

Executes closure after every subsequent KEY event.

afterDelay(tickCount, closure)

Executes closure once after exactly tickCount TICK events have occurred.

twice(closure)

Immediately executes closure two times.

Multiple closures can be registered to handle the same event. If this happens, they execute in the order they were registered.


Part 1

Consider the following code:

A
afterDelay(4, () -> {
    B
    onKeyPress(() -> {
        C
        onClick(() -> {
            D
        });
        E
    });
    F
    afterDelay(1, () -> {
        G
        onClick(() -> {
            H
        });
        I
    });
    J
});
K
afterDelay(2, () -> {
    L
    afterDelay(2, () -> {
        M
        onClick(() -> {
            N
        });
        O
    });
    P
    onKeyPress(() -> Q);
    R
});
S
twice(() -> {
    T
    onKeyPress(() -> {
        U
        onKeyPress(() -> {
            V
        });
        W
    });
    X
    afterDelay(2, () -> Y);
    Z
});
AA
onClick(() -> {
    AB
    afterDelay(1, () -> {
        AC
        onKeyPress(() -> {
            AD
        });
        AE
    });
    AF
    onKeyPress(() -> {
        AG
        onClick(() -> {
            AH
        });
        AI
    });
    AJ
});
AK
afterDelay(5, () -> {
    AL
    afterDelay(1, () -> {
        AM
        onClick(() -> {
            AN
        });
        AO
    });
    AP
    onClick(() -> {
        AQ
        onKeyPress(() -> {
            AR
        });
        AS
    });
    AT
});
AU

Write out the order in which all the marked statements will execute given the following sequence of events (showing both the marked statements and the event names):

TICK
CLICK
KEY
KEY
CLICK
TICK
TICK
TICK
TICK
CLICK
CLICK
TICK
KEY
TICK
TICK
CLICK
TICK
TICK
TICK

Solution

A
T
T

TICK

CLICK
AB

KEY
U
U
AG

KEY
U
U
AG
V
V

CLICK
AB
AH
AH

TICK

TICK
L
Y
Y
AC
AC

TICK

TICK
B

CLICK
AB
AH
AH

CLICK
AB
AH
AH

TICK
AL
M

KEY
U
U
AG
V
V
V
V
AG
Q
AD
AD
C
AG
AG

TICK
G
AC
AC

TICK
AM

CLICK
AB
AH
AH
AQ
N
AH
AH
D
AH
AH
H
AN

TICK

TICK
AC

TICK

AFTER
K
S
X
Z
X
Z
AA
AK
AU
AF
AJ
W
W
AI
W
W
AI
AF
AJ
P
R
AE
AE
F
J
AF
AJ
AF
AJ
AP
AT
O
W
W
AI
AI
E
AI
AI
I
AE
AE
AO
AF
AJ
AS
AE

Part 2

Consider the following code:

public class StatefulPuzzle {
    private int x = 0;

    public void run() {
        onClick(() -> {
            x++;
            System.out.println("x=" + x);
        });

        onClick(() -> {
            if (x < 1) {
                System.out.println("Hi!");
            }
        });

        if (x < 1) {
            onClick(() -> {
                System.out.println("Bye!");
            });
        }

        System.out.println("End of run()");
    }
}

Given a call to run() followed by two CLICK events, what would this code print?

Solution

End of run()
x=1
Hi!
x=2
Hi!

Related puzzles: