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
Clickwhen pressed. The middle component maintains an internal counter. The counter is incremented and output to the display when a
Clickis received from the button.
mapF, allows messages sent between fudgets to be processed in an application specific way.
mapstateFallows 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.
mapstateFhas 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
countis the state transition function in this program.