5.3.3
FrTime: A Language for Reactive Programs
The frtime language supports declarative construction
of reactive systems in a syntax very similar to that of Racket. It
extends racket.
Within DrRacket, as an alternative to using #lang frtime, you can choose FrTime from the
Choose Language menu.
1 Primitives
stands for an undefined value.
returns #t iff
val is a behavior (a time-varying value whose current value can be
projected at any time).
returns #t iff
val is an event (a time-varying stream of values that can occur
at arbitrary times).
is an event that never occurs.
2 Defining Custom Input Signals
returns a
signal whose values initially track that of
init-expr, but
that may be rewired to a different signal by
set-cell!.
rewires
cell (which must have been created by
new-cell) to
take on the value(s) of
val.
returns an event stream that can be
triggered imperatively by
send-event.
3 Signal-Processing Procedures
projects the current value of
a behavior or constant.
delays val by duration milliseconds.
computes
a numeric approximation of the integral of val with respect
to time (measured in milliseconds).
computes a numeric
approximation of the derivative of val with respect to time.
returns an event stream that fires whenever ev fires, whose
values are transformed by application of proc.
returns an event stream that passes through only the values from
ev for which pred returns #t.
merges all of the input
event sources into a single event source.
returns an event source that
carries only the first occurrence of ev. (The rest are
filtered out.)
returns an event source
that occurs each time the argument behavior changes. The value of the
occurrence is the behavior’s new value.
constructs a behavior that starts out as init and then
takes on the last value produced by ev
returns a behavior that starts as init. Each time
ev yields a (potentially time-varying) value, the behavior
switches to that value.
constructs an event source
by accumulating changes (carried by the given event source) over an
initial value.
is similar to
accum-e, except the transformer
function is fixed and is applied to the event occurrence and the
current accumulator (in that order).
returns an event stream that
carries an occurrence each time val changes from #f to
anything else.
provides a mechanism for applying ordinary Scheme primitives to
behaviors. If any of the
vals are behaviors, returns a
behavior whose current value is always equal to
(proc (value-now arg) ...). In FrTime, many Racket primitives are
implicitly lifted.
The following forms allow importation of lifted procedures that aren’t
included in the basic FrTime language.
4 Fred: Functional Reactive Wrapper around GRacket
The constructor arguments are as in
frame%, except that
shown
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying.
The constructor
arguments are the same as in
message%, except that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying.
The constructor
arguments are the same as in
message%, except that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying.
returns an event stream that yields a
value whenever the user clicks the button.
The constructor arguments
are the same as in
check-box%, except that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the check box’s state to that of the event value.
returns a value that always reflects
the current state of the check box.
(new ft-slider% | | | | [label label] | | | | [min-value min-value] | | | | [max-value max-value] | | | | [parent parent] | | | [ | [init-value init-value] | | | | [style style] | | | | [font font] | | | | [enabled enabled] | | | | [vert-margin vert-margin] | | | | [horiz-margin horiz-margin] | | | | [min-width min-width] | | | | [min-height min-height] | | | | [stretchable-width stretchable-width] | | | | [stretchable-height stretchable-height] | | | | [value-set value-set]]) | |
|
→ (is-a?/c ft-slider%) |
label : (or/c label-string? behavior? false/c) |
min-value : (integer-in -10000 10000) |
max-value : (integer-in -10000 10000) |
|
init-value : (integer-in -10000 10000) = min-value |
| style | | : | | (listof (one-of/c 'horizontal 'vertical 'plain | 'vertical-label 'horizontal-label | 'deleted)) |
| | | | = | | '(horizontal) |
|
font : (is-a?/c font%) = normal-control-font |
enabled : any/c = #t |
vert-margin : (integer-in 0 1000) = 2 |
horiz-margin : (integer-in 0 1000) = 2 |
min-width : (integer-in 0 10000) = graphical-minimum-width |
min-height : (integer-in 0 10000) = graphical-minimum-height |
stretchable-width : any/c = (memq 'horizontal style) |
stretchable-height : any/c = (memq 'vertical style) |
value-set : event? = never-e |
The constructor arguments
are the same as in
check-box%, except that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the slider’s state to that of the event value.
returns a value that always reflects
the current state of the slider.
The
constructor arguments are the same as in
check-box%, except
that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the text field’s state to that of the event
value.
returns a value that always reflects
the current state of the text field.
The
constructor arguments are the same as in
check-box%, except
that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the text field’s state to that of the event
value.
returns a value that always reflects
the currently selected element in the radio box.
The
constructor arguments are the same as in
check-box%, except
that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the text field’s state to that of the event
value.
returns a value that always reflects
the currently selected element in the choice control.
The
constructor arguments are the same as in
check-box%, except
that
label,
enabled,
stretchable-width, and
stretchable-height may be time-varying. Also, any occurrence
on
value-set sets the text field’s state to that of the event
value.
returns a value that always reflects
the primary selection in the list box.
returns a value that always reflects
the current set of selected elements in the list box.
5 Graphical Demo Programs
TODO: document the animation library itself!
To run the following animation/GUI demos, simply set the language
level to FrTime, open the corresponding file, and Execute. See the
demo source code for more information.
"orbit-mouse.rkt" : A collection of balls that move in circles
around the mouse pointer.
"piston.rkt" : Simulation of a piston/cylinder.
"rotation.rkt" : Balls moving in circles.
"delay-mouse.rkt" : A trail of balls following the mouse.
"ball-on-string.rkt" : A ball chasing the mouse.
"pong.rkt" : A simple pong/air-hockey game. The left paddle moves with
numeric keypad; the right paddle moves with the mouse. The ’r’ key
resets the score.
"pizza.rkt" : A simple "pizza ordering" user interface based on an HtDP
exercise.
"calculator.rkt" : A simple calculator interface, also based on an HtDP
exercise except that the result updates continuously as the arguments
and operator change.
The next three animation examples are courtesy of Robb Cutler:
"analog-clock.rkt" : An animated real-time clock. A slider adjusts the
radius of the face. Click and drag to move the face around.
"growing-points.rkt" : A field of points that grow as the mouse
approaches.
"needles.rkt" : A field of needles that point at the mouse.