Hi everyone,

I'm trying to convince a extension for Snap to work, but unfortunately
my Haskell-fu is letting me down. I was wondering if anyone could help
provide a pointer in the right direction.

The premise is a very simple hit counter; to start with, I'm manually
incrementing the hits. I based it entirely on the Timer example -- I
think I'm just missing an instance of something somewhere.

I've added a HitCounterState to my ApplicationState, added an instance
of HasHitCounterState to ApplicationState, use hitCounterInitializer in
applicationInitializer. This is all fine. The monad for the HitCounter
is as follows:

> class MonadSnap m => MonadHitCounter m where
>     getHits :: m Int
>     incrementHits :: m ()

Utterly basic. In the implementation, HitCounterState is as follows:

> newtype HitCounterState = HitCounterState
>     { _hits :: Int
>     }

And the initializer:

> hitCounterInitializer :: Int -> Initializer HitCounterState
> hitCounterInitializer n = return n >>= mkInitializer . HitCounterState

My final problem is the simple matter of implementing the logic of
updating state. getHits is simple enough:

> instance HasHitCounterState s => MonadHitCounter (SnapExtend s) where
>     getHits = fmap _hits $ asks getHitCounterState

And likewise for MonadHitCounter (ReaderT s m). The issue is in an
attempt at incrementHits:

>     incrementHits = do
>         hits <- getHits
>         tell setHitCounterState { _hits=hits }

Even this is clunky and possibly wrong, but I can't use trial and error
to discover the best way to lay this out, because (of course) MonadSnap
doesn't include MonadWriter or a StateT or ... well, whatever.

I'm sorry to trouble you with such a beginner's question -- I come from
a non-Haskell FP background, so these matters trip me up.

Thanks,

Tacit

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap

Reply via email to