import sodium.*;
import java.util.Optional;

public class pause {
    public static <A> Stream<Unit> changeTo(Cell<A> a, A target) {
        return Stream.filterOptional(
            Operational.updates(a).snapshot(a,
                (neu, old) -> neu.equals(target) && !neu.equals(old)
                    ? Optional.of(Unit.UNIT)
                    : Optional.empty()
            ));
    }

    public static Cell<Double> pausableClock(Stream<Unit> sPause,
            Stream<Unit> sResume, Cell<Double> clock) {
        Cell<Optional<Double>> pauseTime =
            sPause.snapshot(clock, (u, t) -> Optional.<Double>of(t))
                .orElse(sResume.map(u -> Optional.<Double>empty()))
                .hold(Optional.<Double>empty());
        Cell<Double> lostTime = sResume.<Double>accum(
            0.0,
            (u, total) -> {
                double tPause = pauseTime.sample().get();
                double now    = clock.sample();
                return total + (now - tPause);
            });
        return Cell.lift((otPause, tClk, tLost) ->
            (otPause.isPresent() ? otPause.get()
                                 : tClk)
            - tLost,
            pauseTime, clock, lostTime);
    }

    public static void main(String[] args) {
        CellSink<Double> mainClock = new CellSink<>(0.0);
        StreamSink<Unit> sPause = new StreamSink<>();
        StreamSink<Unit> sResume = new StreamSink<>();
        Cell<Double> gameClock = pausableClock(sPause, sResume, mainClock);
        Listener l = Cell.lift((m, g) -> "main="+m+" game="+g,
                               mainClock, gameClock)
                         .listen(txt -> System.out.println(txt));
        mainClock.send(1.0);
        mainClock.send(2.0);
        mainClock.send(3.0);
        sPause.send(Unit.UNIT);
        mainClock.send(4.0);
        mainClock.send(5.0);
        mainClock.send(6.0);
        sResume.send(Unit.UNIT);
        mainClock.send(7.0);
        l.unlisten();
    }
}


@@ -63,336 +63,8 @@
 e %7B%0A
-    public static %3CA%3E Stream%3CUnit%3E changeTo(Cell%3CA%3E a, A target) %7B%0A        return Stream.filterOptional(%0A            Operational.updates(a).snapshot(a,%0A                (neu, old) -%3E neu.equals(target) && !neu.equals(old)%0A                    ? Optional.of(Unit.UNIT)%0A                    : Optional.empty()%0A            ));%0A    %7D%0A%0A
     

Fix a book example.