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

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
KEY
KEY
CLICK
KEY
CLICK
CLICK
TICK
TICK
TICK
CLICK
TICK
TICK
TICK
TICK
KEY
TICK

Solution

A
V
AA
AA
V
AA
AA

TICK

KEY
AF
AG
AG

KEY
AF
AG
AG
AH
AH

CLICK
AB
AB
AB
AB
AN
AS
AT
AT
AS
AT
AT

KEY
AF
AG
AG
AH
AH
AH
AH

CLICK
AB
AB
AB
AB
AN
AS
AT
AT
AS
AT
AT

CLICK
AB
AB
AB
AB
AN
AS
AT
AT
AS
AT
AT

TICK
B
C
C
W
W

TICK
AK
AK
AK

TICK
L
AO
AO
AO
G

CLICK
AB
AB
AB
AB
AN
AS
AT
AT
AS
AT
AT
X
X
M
N
N

TICK

TICK

TICK
Q
R
R
AO

TICK

KEY
AF
AG
AG
AH
AH
AH
AH
AH
AH
D
D
AP
AP
AP
H
AP

TICK

AFTER
K
U
Z
AC
AC
AD
Z
AC
AC
AD
AE
AM
AW
AI
AI
AJ
AL
AI
AI
AJ
AL
AR
AU
AU
AV
AI
AI
AJ
AL
AR
AU
AU
AV
AR
AU
AU
AV
E
E
F
J
Y
Y
P
T
AQ
AQ
AQ
I
AR
AU
AU
AV
O
S
AQ
AI
AI
AJ
AL

Part 2

Consider the following code:

public class StatefulPuzzle {
    private int x = 0;

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

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

        onClick(() -> {
            if (x > 1) {
                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: