This program illustrates a more general way to combine application specific code with GUI elements from the Fudget library. It illustrates that state information can be encapsulated.
This program has a button and a numeric display. Pressing the button increments the number in the display.
The application specific code in this example sits between the button and the display. It internally maintains a counter which is incremented and output to the display whenever a click is received from the button.
Here is the source code:
import Fudgets main = fudlogue (shellF "Up Counter" counterF) counterF = intDispF >==< mapstateF count 0 >==< buttonF "Up" count n Click = (n+1,[n+1])
counterF
)
is a serial compositon of three parts.
At the output end we see the familiar
intDispF
.
At the input end of the pipe line is a button created with
buttonF
.
It outputs a Click
when pressed. The middle component
maintains an internal counter. The counter is incremented and
output to the display when a Click
is received
from the button.
mapstateF
,
like
mapF
,
allows messages
sent between fudgets to be processed in an application specific
way. mapstateF
allows you to output an arbitrary
number of messages as response to an input message. In addition
the output can depend on not only the current input, but also on
an internal state. mapstateF
has two arguments:
a state transition function and an initial state.
When applied to the current state and and an
input message, the state transition function should produce a
new internal state and a list of output messages.
The function count
is the state transition function
in this program.