[Haskell-cafe] Functional references

Ryan Ingram ryani.spam at gmail.com
Thu Sep 4 22:36:55 EDT 2008

Nice.  I've written similar stuff a couple of times before, but the
formulation using Maybe and modify definitely solves some problems I
started to notice as I used it on bigger structures.  However, it
might be better to separate a class of "never failing" references,
where the reader is guaranteed to succeed, from "potentially failing"
references which give a Maybe value.

I'd also not use the names "get" and "modify" because they are already
used by MonadState.    I always used "frGet", "frSet", and "frModify",
and then tended to not use them directly as I was generally working in
a state monad.

Here's a few more primitives that I found quite useful:

> fetch :: MonadState s m => FRef s a -> m (Maybe a)
> fetch ref = liftM (frGet ref) get

> (=:) :: MonadState s m => FRef s a -> a -> m ()
> r =: v = modify (frSet r v)

> ($=) :: MonadState s m => FRef s a -> (a -> a) -> m ()
> r $= f = modify (frModify r f)

You should package this up and put it on hackage.

  -- ryan

On Thu, Sep 4, 2008 at 4:51 PM, Tim Newsham <newsham at lava.net> wrote:
> I'm playing with functional references and looking for some feedback on a
> small FRec library I put together:
>  http://www.thenewsh.com/~newsham/FRef.hs
> Novel (I think) is that the library is applied to some data accesses that
> are not normally covered by functional references -- ie. extracting words or
> replacing words in a string or values in an association list.  I'm hoping it
> will provides a useful framework for editing complex values such as data
> embedded in Base64 cookies in an HTTP header.
> Tim Newsham
> http://www.thenewsh.com/~newsham/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list