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

Solution

A

TICK

CLICK
AF

KEY
V
W
W
AN
AG

TICK

KEY
V
W
W
AN
AG
AO

TICK
L
M
N
N
M
N
N

KEY
V
W
W
AN
AG
AO
AO

TICK
AI
AA
AQ

KEY
V
W
W
AN
AG
AO
AO
AO
AJ
AB

TICK
AA
AQ

TICK
B
C
D
D
C
D
D
Q
R
R
AA
AQ

CLICK
AF
X
X
X
X
X
X
AR
X
X
AR
AR

TICK
AA
AQ

TICK
G
H
H

CLICK
AF
X
X
X
X
X
X
AR
X
X
AR
AR
AR

KEY
V
W
W
AN
AG
AO
AO
AO
AJ
AB
AO
AB
AB
AG
AB
AG

TICK
AI

TICK

TICK
AI
AA
AQ

AFTER
K
U
AE
AM
AU
AH
AL
Y
Y
Z
AD
AP
AT
Y
Y
Z
AD
AP
AT
O
O
P
T
Y
Y
Z
AD
AP
AT
AK
AC
AS
Y
Y
Z
AD
AP
AT
AC
AS
E
E
F
J
S
AC
AS
AH
AL
AC
AS
I
AH
AL
Y
Y
Z
AD
AP
AT
AK
AK
AC
AS

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 > 0) {
                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: