[GUI] Events/Finalizers/Streams
George Russell
ger@tzi.de
Tue, 11 Mar 2003 16:58:37 +0100
Nick wrote, quoting me, snipped
>> For example, will this stream library also subsume CML style events
>> (as implemented by me as part of HTk), which allow you to wait on
>> multiple events and return when any one of them occurs?
>
> Well, this is an example wich requires concurrency. "Merge" of events
> will be available only on preemptive multithreaded implementation,
> unless I am wrong and there is an alternative way (like a "select"
> operator).
OK, with higher-order events, as invented by John Reppy (CML) and
implemented by me in Haskell, you do have a select operator, and you
do not need pre-emptive multithreading. (Obviously such things are
a good thing anyway, but they are not essential.) So if an
Event a
is an event carrying a value of type "a", there is a function
(+>) :: Event a -> Event a -> Event a
which selects either of the two events. There is also another operator
(>>>=) :: Event a -> (a -> IO b) -> Event b
allowing you to postprocess the result of an event in some way.
For example if you have two buttons, a "Yes" button with a click event
(yes :: Event ()), and a "No" button with a click event (no :: Event ())
then you can define construct
yesOrNo :: Event Bool
yesOrNo =
(yes >>>= (\ _ -> return True)
+> (no >>>= (\ _ -> return False)
Then
sync yesOrNo
will wait for the next button click, and tell you if it was of "Yes" or
"No".