[Haskell-beginners] firering event in netwire

Ertugrul Söylemez es at ertes.de
Sat Sep 29 08:56:55 CEST 2012

Nathan Hüsken <nathan.huesken at posteo.de> wrote:

> How do I fire events?
> For example my main wire should take keyboard events as input and
> output some state (which is than somehow outputed).

The idea is that you either provide the decision of whether to fire an
event through the input (direct style) or you use an underlying monad
(indirect style).  The direct style would have this type:

    keyDown :: (Monoid e, Reactive cat) =>
               KeySym -> Event e cat SDL.Event

The indirect style is nicer to use, but requires an underlying monad:

    keyDown :: (Monoid e) => KeySym -> Event e (Kleisli MyMonad) a

> I see that there is an event type, which has the some type as the
> identiy wire. I am just not sure how to use it?

The idea is that event wires should act like the identity wire, if an
event has happened and inhibit otherwise.  That way you can easily put
it into a wire composition:

    time . periodically 1

> Or asked differently, how is the "keyDown" event defined, that was
> talked of in an earlier mail?

In the direct case you can define it as a simple composition:

    isSym :: KeySym -> SDL.Event -> Bool
    isSym s (KeyDown s') = s == s'
    isSym _ _ = False

    keyDown = require . isSym

In the indirect case you can use 'mkFixM' to construct your own wire:

    keyDown sym =
        mkFixM $ \_ x -> do
            ev <- asks currentEvent
            return $
                case ev of
                  KeyDown sym' | sym == sym' -> Right x
                  _                          -> Left mempty

I hope this helps.


Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120929/8af0952c/attachment.pgp>

More information about the Beginners mailing list