is there a more concise way to generate helper functions for a datatype built on records?

I think I'm running into more or less the same issue discussed at http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy... Just wondering if I missed anything, or if any of the ideas considering better records setter/getters have been implemented in the meantime. t. ************** -- Is there any way to do the following more concisely? -- Seems like a lot of boilerplate data GameState = GameState { guesses :: Int, userHighScores :: UserHighScores, answer :: Maybe Int } deriving Show -- State Helpers --------------------------------------- modGuesses f gSt = _set_guesses ( (f . guesses) gSt) gSt modUserHighScores f gSt = _set_userHighScores ( (f . userHighScores) gSt) gSt modAnswer f gSt = _set_answer ( (f . answer) gSt) gSt _set_guesses new gSt = gSt {guesses=new} _set_userHighScores new gSt = gSt { userHighScores=new } _set_answer new gSt = gSt { answer=new }

Hi
Some of these can be automatically derived by the Data.Derive tool. If
you want any more, then submit a patch and they can all be derived.
http://www-users.cs.york.ac.uk/~ndm/derive/
The derivations Set, Is, From, Has, LazySet all look useful. A bit
more documentation on what each one does would also be handy, if
anyone wants to write it!
Thanks
Neil
On Nov 24, 2007 4:01 PM, Thomas Hartman
I think I'm running into more or less the same issue discussed at
http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy...
Just wondering if I missed anything, or if any of the ideas considering better records setter/getters have been implemented in the meantime.
t.
**************
-- Is there any way to do the following more concisely? -- Seems like a lot of boilerplate
data GameState = GameState { guesses :: Int, userHighScores :: UserHighScores, answer :: Maybe Int } deriving Show
-- State Helpers --------------------------------------- modGuesses f gSt = _set_guesses ( (f . guesses) gSt) gSt modUserHighScores f gSt = _set_userHighScores ( (f . userHighScores) gSt) gSt modAnswer f gSt = _set_answer ( (f . answer) gSt) gSt
_set_guesses new gSt = gSt {guesses=new} _set_userHighScores new gSt = gSt { userHighScores=new } _set_answer new gSt = gSt { answer=new } _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I think records are grungy, too.
I'm not sure what available options there are on Hackage, but here are
some of my attempted solutions:
http://luqui.org/blog/archives/2007/08/05/haskell-state-accessors-second-att...
And here's the automatic derivation module I said I'd write in the post:
http://svn.luqui.org/svn/misc/luke/work/code/haskell/accessor/
It's all experimental, but it seems to do the trick pretty nicely.
Luke
On Nov 24, 2007 4:01 PM, Thomas Hartman
I think I'm running into more or less the same issue discussed at
http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy...
Just wondering if I missed anything, or if any of the ideas considering better records setter/getters have been implemented in the meantime.
t.
**************
-- Is there any way to do the following more concisely? -- Seems like a lot of boilerplate
data GameState = GameState { guesses :: Int, userHighScores :: UserHighScores, answer :: Maybe Int } deriving Show
-- State Helpers --------------------------------------- modGuesses f gSt = _set_guesses ( (f . guesses) gSt) gSt modUserHighScores f gSt = _set_userHighScores ( (f . userHighScores) gSt) gSt modAnswer f gSt = _set_answer ( (f . answer) gSt) gSt
_set_guesses new gSt = gSt {guesses=new} _set_userHighScores new gSt = gSt { userHighScores=new } _set_answer new gSt = gSt { answer=new } _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 11/25/07, Thomas Hartman
I think I'm running into more or less the same issue discussed at
http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy...
Just wondering if I missed anything, or if any of the ideas considering better records setter/getters have been implemented in the meantime.
Have a look at [http://code.haskell.org/category], and the associated blog posts http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.detai... (http://tinyurl.com/2ustba) and http://twan.home.fmf.nl/blog/haskell/References-Arrows-and-Categories.detail... (http://tinyurl.com/2v8het) which discuss "functional references" (similar to Luke's), and include Template Haskell code for deriving more flexible accessors from a record declaration. Also check out HList [http://homepages.cwi.nl/~ralf/HList/], which can do some interesting things, provided you're willing to abandon the built-in record system. Stuart

There is a simplified version of HList style functionality inside HAppS-Data because I found Oleg's repo too hard to understand. -Alex- Stuart Cook wrote:
On 11/25/07, Thomas Hartman
wrote: I think I'm running into more or less the same issue discussed at
http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy...
Just wondering if I missed anything, or if any of the ideas considering better records setter/getters have been implemented in the meantime.
Have a look at [http://code.haskell.org/category], and the associated blog posts
http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.detai... (http://tinyurl.com/2ustba)
and
http://twan.home.fmf.nl/blog/haskell/References-Arrows-and-Categories.detail... (http://tinyurl.com/2v8het)
which discuss "functional references" (similar to Luke's), and include Template Haskell code for deriving more flexible accessors from a record declaration.
Also check out HList [http://homepages.cwi.nl/~ralf/HList/], which can do some interesting things, provided you're willing to abandon the built-in record system.
Stuart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Alex Jacobson
-
Luke Palmer
-
Neil Mitchell
-
Stuart Cook
-
Thomas Hartman