Download Functional Reactive Programming for Robotics with Elm Abstract

Transcript
12
Chapter 2. Theoretical Considerations
described above. We will now describe the behaviour of each individual function in
this example in detail.
The function startWith takes the Signal String representing the current
time, and produces a value signal which is identical to its input, and a switch signal
which is always True.
startWith
The first until function takes the output of startWith and the condition
As its output value signal it will always emit its input value signal
unchanged. For its output switch signal,
First until
Keyboard.enter.
• While the condition is False (i.e. the user has not pressed Enter yet), it will emit
False.
• After the condition becomes True for the first time (i.e. the user pressed Enter),
it will start emitting True indefinitely.
The andThen function takes the output of until and a new value signal of
the same type as a, in our case, String. As its output switch signal, it will always emit
its input switch signal unchanged. For its output value signal,
andThen
• While its input switch signal is False (i.e. the previous until is still emitting
False, because the user has not pressed Enter yet), it will emit its input value
signal unchanged - in our case, the current time.
• When its input switch signal becomes True (i.e. because the previous until
started to emit True, because the user pressed Enter), it will start emitting the
new value signal indefinitely - in our case, the mouse position. Note that the
new switch signal is being passed through.
Second until The second until will have three states. Just as the first until, it will
always pass through the value of its value signal. For its output switch signal,
• Before the first Enter is pressed, it is simply passing through the switch signal
unchanged.
• After the first Enter is pressed, it is still passing through its switch signal
unchanged, but it is starting to observe its condition. Also, note that the new
value signal is being passed through.
• After Enter is pressed again, it will start emitting True as its switch signal.
The final function, andEternally, unwraps the switch signal - value
signal tuple into the overall value signal. It simply passes through its input value
signal while its switch signal is False, and starts emitting the new value signal once
its switch signal becomes True.
andEternally